我有两个型号,用户和产品
class User < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :users
end
现在我想为每个用户制作三个列表,他们最近检查过的产品,购物车中的产品以及他们购买的产品。我如何区分这些关系?我可以在关系表中添加某种类型的列吗?然后我怎么能检查这种类型呢?
由于
答案 0 :(得分:1)
如果您要在关系表中添加其他列,或许您应该考虑在用户和产品上使用has_many,那么您可以在购物车上添加列
class User < ActiveRecord::Base
has_many :carts
has_many :products, through: :carts
end
class Cart < ActiveRecord::Base
belongs_to :user
belongs_to :product
end
class Product < ActiveRecord::Base
has_many :carts
has_many :users, through: :carts
end