我有3个模型:
class Product < ActiveRecord::Base
has_many :product_recommendations, :dependent => :destroy
has_many :recommendations, :through => :product_recommendations
end
class Recommendation < ActiveRecord::Base
has_many :product_recommendations, :dependent => :destroy
has_many :products, :through => :product_recommendations
end
class ProductRecommendation < ActiveRecord::Base
belongs_to :recommendation
belongs_to :product
end
我还有一个创建新推荐的方法:
def self.get_recommendation(product, rating_set)
r = Recommendation.create
r.title = @title
r.description = @description
r.cover_img = @image_url
r.curr_price = @curr_item_price
r.save!
end
我在调用该方法时传入了产品对象和 rating_set 属性。
如何创建与 1) 产品对象的 product_recommendation 关联。2)新创建的推荐对象。3) 属于 ProductRecommendation 连接表的附加属性(rating_set)。
我尝试了以下没有运气:
product.create_product_recommendations( {:rating_set => rating_set}, {:recommendation => r} )
product.recommendations.create( :rating_set => rating_set, :recommendation => r )