2016-04-01 16 views
0

Amaç, aşağıdaki sql komutunun kullandığı bir tabloyu çoğaltmaktır. .. "Ürünler" INNER JOIN "products_promotion_rules" "ürünler" AÇIK "id" = "products_promotion_rules" "product_id" "ürünler" DANBu sql aramayı kullanan raylarda bir kopya ilişkisi oluşturma

SEÇ "ürünler" *:

SQL komutu. . "deleted_at" IS NULL AND "products_promotion_rules" "promotion_rule_id" = $ 1

Benim Raylar dernek şöyle görünür:.

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 

Yukarıdaki rayları kullanmayı denedim, ancak bu product_promotion_rules tablosunun bulunmadığına dair bir hata alıyorum, ayrıca Promotion::Rules::ProductValue::ProductPromotionRule is an uninitialized constant.

+0

, "productsPromotionRule" ürününün "products_promotion_rules" adlı bir tablo adı olmasını istiyorsunuz. İlişkilendirme adlarını –

cevap

0

has_and_belongs_to_many'u yeniden oluşturmaya çalışıyorsunuz gibi görünüyor. Bu senin products_promotion_rules tablonun varlığını algılar ve ProductPromotionRule modeli, sadece Product ve PromotionRule gerekmez:

class Product 
    has_and_belongs_to_many :promotion_rules 
end 

class PromotionRule 
    has_and_belongs_to_many :products 
end 

Bu sizin şema yerde zaten benziyor ve bu uymalıdır:

Table "public.products" 
Column | Type | Modifiers 
--------+---------+----------- 
id  | integer | 

Table "public.promotion_rules" 
Column | Type | Modifiers 
--------+---------+----------- 
id  | integer | 

Table "public.products_promotion_rules" 
     Column  | Type | Modifiers 
-------------------+---------+----------- 
product_id  | integer | 
promotion_rule_id | integer | 
+0

güncellemeniz gerekir. Bunu uygularken Promotion_rules_rules bulunmadığı bir hatadır. –

İlgili konular