开发者

Active record query in rails with conditional join

开发者 https://www.devze.com 2023-04-11 14:30 出处:网络
I have models Category and Deal and having has_many :through mapping via categories_deals. I have a another model, City, which ha开发者_高级运维s a has_many :through mapping via cities_dealswith dea
  • I have models Category and Deal and having has_many :through mapping via categories_deals.
  • I have a another model, City, which ha开发者_高级运维s a has_many :through mapping via cities_deals with deals.

Now I want to fetch deals in category 2 and 3 and city 10.

CategoriesDeal.where(:category_id=>[2,3])

Like how to specify city now.


I figured out the solution.. Here it is:

Deal.all(:joins=>[:cities, :categories], :conditions=>["cities.id= ? and categories.id IN (?)",2, [62,43]]).uniq
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号