想象一下模型结构如下:
class CrossSellPromotion < Promotion
has_and_belongs_to_many :afflicted_products, :join_table => :promotion_afflicted_products, :foreign_key => 'promotion_id', :association_foreign_key => 'product_id', :class_name => 'Product'
has_and_belongs_to_many :afflicted_categories, :join_table => :promotion_afflicted_categories, :foreign_key => 'promotion_id', :association_foreign_key => 'category_id', :class_name => 'Category'
. . .
end
class Promotion < ActiveRecord::Base
has_and_belongs_to_many :products, :join_table => :promotions_products
has_and_belongs_to_many :categories, :join_table => :promotions_categories
- - -
end
和表格如下:
table promotions
type :string
...some other fields unrelated to this problem
table promotions_products
promotion_id :integer
product_id :integer
table promotion_afflicted_products
promotion_id :integer
product_id :integer
table promotion_afflicted_categories
promotion_id :integer
category_id :integer
然后我有一个固定装置如下:
#promotions.yml
cross_sell_1:
products: plumeboom_1, plumeboom_2
value: 100
value_type: percentage
type: CrossSellPromotion
#products.yml
plumeboom_1:
model: plumeboom1
url: plumeboom-1
plumeboom_2:
model: plumeboom2
url: plumeboom-2
plumeboom_3:
model: plumeboom3
url: plumeboom-3
当我运行单元测试时,它会返回以下错误消息:
1) Error:
test_the_truth(CartTest):
NoMethodError: undefined method `singularize' for :promotions_products:Symbol
请让我知道,如果您碰巧有类似的经历或知道如何解决这个问题,或者至少只是您认为可能出错的事情,我准备尝试任何事情了!
非常感谢,请不要犹豫回复......真的很绝望!
答案 0 :(得分:3)
答案是......你不能!
可悲的是,有时候Rails不会削减它。在这些情况下,只需为关联表创建另一个Fixture。
E.g。在这种情况下,promotion_products.yml并在其中输入:
pp1:
promotion_id: <%= Fixtures.identify(:cross_sell_1) %>
product_id: <%= Fixtures.identify(:plumeboom_1) %>
我设法在灯具中使用HABTM连接但是当它们也有STI(单表继承)时,它似乎有问题。