我有3个型号:
活动,地点,位置
Event:
belongs_to :place
Place:
belongs_to :location
Location:
has_many :places
我想查找Location的city
属性为'New York'的所有事件。使用Rails4和Postgres的最佳方法是什么?
答案 0 :(得分:2)
这应该
Event.includes({ place: :locations }).where(locations: {city: 'New York'}).references(:locations)
答案 1 :(得分:1)
尝试
Event.joins(:place => :location).where("locations.city = ?", "New York")
答案 2 :(得分:0)
你想要使用nested associations(特别是第12.2.3节)
Event.joins(places: :locations).where('locations.city = ?', 'New York')