activerecord-通过相关记录的属性进行查询

activerecord-通过相关记录的属性进行查询,activerecord,Activerecord,我希望能够查询 class Book belongs_to :library end class Library belongs_to :city has_many :books end class City has_many :libraries has_many :books, through: :library end 如何使用ActiveRecord正确地做到这一点?您的城市模型已经有许多图书通过图书馆,所以我相信 Book.where("library.cit

我希望能够查询

class Book
  belongs_to :library
end

class Library
  belongs_to :city
  has_many :books
end

class City
  has_many :libraries
  has_many :books, through: :library
end

如何使用ActiveRecord正确地做到这一点?

您的
城市
模型已经有许多图书通过图书馆,所以我相信

Book.where("library.city.name = ?", "Alexandria")

你应该做这个把戏,你的
城市
模型已经有很多书通过图书馆,所以我相信

Book.where("library.city.name = ?", "Alexandria")

应该做这个把戏

天哪,这是一个很老的问题,不管怎样,如果有人偶然发现这个问题,我会把答案留在那里。天哪,这是一个很老的问题,不管怎样,如果有人偶然发现这个问题,我会把答案留在那里