SQL - Rails深度嵌套连接



首先,我是法国人,很抱歉我的英语很丑。

我有以下模型:

has_and_belongs_to_many :products
产品

has_and_belongs_to_many :shops
has_many :taggings
has_many :tags, through: :taggings

标记

belongs_to :tag
belongs_to :product

标记

has_many :taggings
has_many :products, through: :taggings

我想能够做Shop.first.tags,所以我想获得所有商店的产品的标签,在一个单一的请求,如果可能的话。如果我能解释一下,那就好了

您没有在Shop模型上指定tags的关系。为了在单个对象上调用#tags,您需要添加该关系。

class Shop
  # ...
  has_many :tags, :through => :products
  # ...
end

最新更新