【问题标题】:SQL - Rails Deep nested joinsSQL - Rails 深度嵌套连接
【发布时间】:2013-03-03 02:34:12
【问题描述】:

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

我有以下型号:

商店

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,所以如果可能的话,我想在一个请求中获取所有商店的产品标签。如果我能解释一下就好了:)

【问题讨论】:

    标签: sql ruby-on-rails join tags include


    【解决方案1】:

    您尚未在 Shop 模型上为 tags 指定关系。您需要添加该关系才能在单个对象上调用 #tags

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

    【讨论】:

    • 太简单了...我尝试做类似 Shop.joins(products: [taggings: :tags])... 谢谢,它有效!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多