从促销规则到产品不起作用的关联

时间:2016-04-01 20:30:40

标签: ruby-on-rails ruby associations spree

我正在尝试通过促销访问产品但不能。

在命令行中:Promotion.last.promotion_rules.first.products

返回未初始化常量的错误。

以下是我的协会:

class Product
   has_many :product_promotion_rules, class_name: 'ProductPromotionRule'
   has_many :promotion_rules, through: :product_promotion_rules
end 

class ProductPromotionRule
   belongs_to :product
   belongs_to :promotion_rule
end

class PromotionRule
   has_many :product_promotion_rules, class_name: 'ProductPromotionRule', join_table: 'products_promotion_rules', foreign_key: :promotion_rule_id
   has_many :products, through: :product_promotion_rules
   belongs_to :promotion 
end

class Promotion
   has_many :promotion_rules
end

1 个答案:

答案 0 :(得分:0)

在产品型号中尝试:

has_many :product_promotion_rules, class_name: 'ProductPromotionRule', foreign_key: :product_id
has_many :promotion_rules, through: :product_promotion_rules, source: :promotion_rule

在PromotionRule模型中:

has_many :products, through: :product_promotion_rules, source: :product

更新:看到了这个,你可能想删除/修复:

join_table: products_promotion_rules

更改为:

join_table: product_promotion_rules

或尝试将其删除。

相关问题