ruby-on-rails - 如何查询双belongs_to关联

标签 ruby-on-rails postgresql ruby-on-rails-4

我有 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/

相关文章:

ruby-on-rails - Activerecord:通过单个查询查找非关联加关联的记录

ruby-on-rails - 如何组织 Rails 应用程序

ruby-on-rails - Ruby on Rails : What id do I want here?

sql - 在 GROUP BY 中使用 SELECT 的 Case 语句的 columnName

windows - psql "more is not recognized"错误

ruby-on-rails - 如何保存我的多对多关系?

ruby-on-rails - 将 onClick 事件附加到 Rails 的 link_to_function

java - 使用 JOOQ 在 "not in"子句中指定多个列进行删除

ruby-on-rails - Ruby of Rails 无法在没有前导字母字符的情况下呈现部分(仅使用数字或前导数字)

ruby - 导轨 4 : activerecord create and update return value?