Rails 单表继承与 HABTM Fixture 在单元测试中返回 NoMethodError: undefined method `singularize;
想象一个模型结构如下:
models/cross_sell_promotion.rb
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
models/promotion.rb
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
请告诉我,如果您碰巧有类似的经历或知道如何解决这个问题,或者至少只是你认为可能是错误的,我准备尝试任何事情!
非常感谢,请毫不犹豫地回复...这里真的很绝望!
Imagine a model structure as follows:
models/cross_sell_promotion.rb
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
models/promotion.rb
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
and tables as follows:
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
Then I have a fixtures as follows:
#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
When I run my unit test, it returns this error message:
1) Error:
test_the_truth(CartTest):
NoMethodError: undefined method `singularize' for :promotions_products:Symbol
Please, let me know if you happen to have similar experience or know how to fix this, or at least just what you thought might be wrong, I am ready to try anything out!
Thanks a lot, please do not hesitate to reply... really desperate here!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是……你不能!
遗憾的是,有时 Rails 就是无法解决这个问题。在这些情况下,只需为关联表创建另一个夹具即可。
例如,在本例中 Promotions_products.yml 并在其中输入:
我设法在固定装置中使用 HABTM 连接,但是当它们也具有 STI(单表继承)时,rails 似乎有问题。
The answer is... You can't!
Sadly but sometimes Rails just won't cut it. In these situations just create another Fixture for association tables.
E.g. in this case promotions_products.yml and inside it you enter:
I managed to use HABTM connections in fixtures but when they are also having STI (single table inheritance) rails seems to have problems with it.