我的模型Rails的创建通过
class Collection < ActiveRecord::Base
has_many :outfits
has_many :products, through: :outfits
end
class Outfit < ActiveRecord::Base
belongs_to :product
belongs_to :collection
end
class Product < ActiveRecord::Base
has_many :outfits
has_many :collections, through: :outfits
end
我想保存在产品集合模型对象的has_many
这样一个集合可以有几个产品在它
我该怎么办呢?我和你在一起有点挣扎
它已经试过这样的事情
p = Product.find_by_code('0339').id
p.collections.create(product_id:p1)
,但我想我错了
你能写出你想建模的关系吗?这会使它更容易理解。例如,收藏品有很多服装,服装有很多产品,但产品只有一种服装? –
@MatthewCliatt我的主要目标是在许多'集合'中有一个'产品' – user
这和服装有什么关系吗? –