我有以下型号:
class Product < ActiveRecord::Base
has_many :product_recommendation_sets, :dependent => :destroy
has_many :recommendation_sets, :through => :product_recommendation_sets
end
class RecommendationSet < ActiveRecord::Base
has_many :product_recommendation_sets, :dependent => :destroy
has_many :products, :through => :product_recommendation_sets
has_many :recommendations
end
class Recommendation < ActiveRecord::Base
belongs_to :recommendation_set
end
我recommendations
recommendations_set
像这样添加:
p = Product.find_by_wmt_id(product) || Product.create( ItemData.get_product_data(product) )
recommendation = find_by_rec_id(rec_id) || create( ItemData.get_product_data(rec_id) )
rec_set = RecommendationSet.find_or_create_by_rating_set_id_and_model_version_and_product_id(rating_set.id, model_version, p.id)
sec_set.update_attributes(
:rating_set_id => rating_set.id,
:product_id => p.id,
:model_version => model_version,
:notes => note
)
sec_set.recommendations << recommendation
sec_set.save
prs = ProductRecommendationSet.find_or_create_by_recommendation_set_id_and_rating_set_id_and_product_id(rec_set .id, rating_set.id, p.id,)
prs.update_attributes(
:recommendation_set_id => rec_set.id,
:rating_set_id => rating_set.id,
:product_id => p.id
)
这可以按预期工作,但是我的问题是我有多个recommendation_sets
属于 multiple products
,并且每个recommendation_sets
可能具有相同的recommendation
. 通过像我目前所做的那样将每个保存recommendation
到 a中,如果两个具有相同的,则只有一组会添加。无论如何都可以使用辅助 id 将每个保存为多个,例如 save by ,还是我需要将此关系更改为 a ?recommendation_set
recommendation_sets
recommendation
recommendation
recommendation
recommendation_sets
recommendation_id_and_product_id
has_many :through