我有 3 个模型:
事件、地点、位置
Event:
belongs_to :place
Place:
belongs_to :location
Location:
has_many :places
我想查找 Location 的 city
属性为“New York”的所有事件。使用 Rails4 和 Postgres 的最佳方式是什么?
最佳答案
应该这样做
Event.includes({ place: :locations }).where(locations: {city: 'New York'}).references(:locations)
关于ruby-on-rails - 如何查询双belongs_to关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34998260/