Rails 单表继承与 HABTM Fixture 在单元测试中返回 NoMethodError: undefined method `singularize;

发布于 2024-08-24 00:38:24 字数 1791 浏览 6 评论 0原文

想象一个模型结构如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝影如岚 2024-08-31 00:38:24

答案是……你不能!

遗憾的是,有时 Rails 就是无法解决这个问题。在这些情况下,只需为关联表创建另一个夹具即可。

例如,在本例中 Promotions_products.yml 并在其中输入:

pp1:
    promotion_id: <%= Fixtures.identify(:cross_sell_1) %>
    product_id: <%= Fixtures.identify(:plumeboom_1) %>

我设法在固定装置中使用 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:

pp1:
    promotion_id: <%= Fixtures.identify(:cross_sell_1) %>
    product_id: <%= Fixtures.identify(:plumeboom_1) %>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文