Rails habtm joins

前端 未结 2 1257
挽巷
挽巷 2021-01-02 10:26

I have this relationship between categories, products & brands:

class Brand < ActiveRecord::Base
  has_many :products
end

class Cate         


        
相关标签:
2条回答
  • 2021-01-02 11:10

    Controversially HABTM's are rarely, if ever, a good design and IMO just about the only thing Rails got wrong.

    Introduce an xref table to join products and categories and use has_many :through on both sides of the relationship so you end up with

    class Brand < ActiveRecord::Base
      has_many :products
      has_many categories :through => products # This is now allowed in Rails 3.x and above
    end
    
    class Category < ActiveRecord::Base
      belongs_to :product_category
      has_many :products :through => product_category 
    end
    
    class Product < ActiveRecord::Base
      belongs_to :brand
      belongs_to :product_category
      has_many :categories :through => product_category
    end
    
    class ProductCategory < ActiveRecord::Base
      has_many :products
      has_many :categories
    end
    

    This gives you the best flexibility with the least amount of code re-factoring for you plus a much more intuitive path to get whatever data you need on either side of the relationship and will enable you to achieve the following

    b = Brand.find(1)
    b.categories.all
    

    Update The above is totally untested code and I have just corrected a glaringly stupid mistake I made. If you have any issues implementing this then come back

    0 讨论(0)
  • 2021-01-02 11:18

    You did the right thing with the join, just add a more complex where definition:

    Category.joins(:products).where(:products => {:brand_id => 1})
    
    0 讨论(0)
提交回复
热议问题