0

所以我试图找到所有标签名称为“学校”的位置。Location has_many Tags 和 Tag has_many Locations via a join table 通过 :through 模型方法访问

class Location < ActiveRecord::Base
  has_many :location_tags
  has_many :locations, :through => :location_tags
end

class LocationTag < ActiveRecord::Base
  belongs_to :location
  belongs_to :tag
end

class Tag < ActiveRecord::Base
  has_many :location_tags
  has_many :locations, :through => :location_tags
end

所以这些是我的模型。我知道解决方案将涉及includesjoins或类似的东西

4

2 回答 2

1

Tag.where(:name => 'school').locations

于 2013-08-16T20:35:09.590 回答
0

知道了。

Location.where(query_stuff).includes(:tags).where(:tags => { :name => "school" })
于 2013-08-27T23:54:47.290 回答