我類別之間的這種關係,產品&品牌:的Rails的habtm加入
class Brand < ActiveRecord::Base
has_many :products
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
belongs_to :brand
end
如何選擇跟這個有關係的所有類別的指定品牌? 我試試這個,但得到一個錯誤
b = Brand.find(1)
Category.joins(:products).where(:products => b.products)