【问题标题】:rails 3.1 inflection problemrails 3.1 拐点问题
【发布时间】:2011-07-06 21:23:40
【问题描述】:

我有一个带有以下 2 个模型的 Rails 3.1 应用程序

class Listing < ActiveRecord::Base
  has_many :listing_saves
end

class Team < ActiveRecord::Base
  has_many :listing_saves
  has_many :saved_listings, through: :listing_saves, source: 'listing'
end

Join 模型如下所示

class ListingSave < ActiveRecord::Base
  belongs_to :team
  belongs_to :listing
end

我认为存在拐点问题,因为每当我尝试运行我的测试时,我都会收到以下错误(这是一个错误示例和导致它的测试)

it "should return the listing saves associated with the team" do
  save = Factory :listing_save, listing: @listing, saver: @user, team: @team
  @team.listing_saves.should include save
end

Failures:

  1) Team listing_saves associations should return the listing saves associated with the team
     Failure/Error: @team.listing_saves.should include save
     NameError:
       uninitialized constant Team::ListingSafe
     # ./spec/models/team_spec.rb:55:in `block (3 levels) in <top (required)>'

好像 Rails 正在将 listing_saves 单数化为 listing_safe

这是我尝试过的一些自定义变形器(不是同时尝试)(它们都不起作用)

# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
  inflect.plural 'saved_listing', 'saved_listings'
  inflect.singular 'saved_listings', 'saved_listing'
  inflect.plural 'listing_save', 'listing_saves'
  inflect.singular 'listing_saves', 'listing_save'
  inflect.singular 'listing_safes', 'listing_safe'
  inflect.plural 'listing_safe', 'listing_safes'
  inflect.irregular 'listing_save', 'listing_saves'
  inflect.irregular 'saved_listing', 'saved_listings'
end

接下来我能做什么?

注意:我找到了this similar question,但答案似乎没有解决我的问题

编辑 我按照下面的答案,所以我现在在我的config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'listing_save', 'listing_saves'
end

当我打开控制台会话并运行 "listing saves".singularize 时,我希望得到“listing_save”。但是,似乎我的应用程序至少有一部分没有得到它,我的测试仍然像以前一样失败。 (我发誓我会在测试/运行应用程序之前重新启动我的服务器和 spork!)。

编辑 2 我在我的应用中写了一些变化测试:

describe "inflection" do
  it "should singularize listing_saves properly" do
    "listing_saves".singularize.should == "listing_save"
  end

  it "should pluralize listing_save properly" do
    "listing_save".pluralize.should == "listing_saves"
  end
end

现在我的情况是这些测试正常通过,但其他测试仍然失败并出现与之前相同的错误

NameError:
       uninitialized constant User::ListingSafe

相同的应用程序,相同的 spork 实例,加载相同的文件。这里发生了什么奇怪的事情!??

【问题讨论】:

  • 也许是个愚蠢的问题,但是......如果它给你带来这么多麻烦 - 为什么不为它找到另一个名字呢? ;)
  • 哈哈是的,当然,我们都知道我不能让它打败我!
  • 在下面查看我的新编辑。看起来屈折在这里不起作用(至少不像预期的那样):-/

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 activesupport inflection


【解决方案1】:

您需要定义一个不规则变形:

# Test your inflections!
> "listing_save".pluralize
=> "listing_saves" # OK!
> "listing_saves".singularize
=> "listing_safe"  # Ouch :(

# Make it smarter
ActiveSupport::Inflector.inflections { |i| 
  i.irregular 'listing_save', 'listing_saves' 
}

# Test again
> "listing_saves".singularize
=> "listing_save"  # Yay!

Ruby 文档:

------------------------ ActiveSupport::Inflector::Inflections#irregular
     irregular(singular, plural)
------------------------------------------------------------------------
     Specifies a new irregular that applies to both pluralization and
     singularization at the same time. This can only be used for
     strings, not regular expressions. You simply pass the irregular in
     singular and plural form.

     Examples:

       irregular 'octopus', 'octopi'
       irregular 'person', 'people'

编辑:

一些进一步的调查 - 看起来其他人也偶然发现了同样的问题(变形与关联没有按预期工作)。所以同时你可以手动设置类名:

has_many :listing_saves, :class_name => "ListingSave"

其他人有同样的问题,并进行了额外的屈折调整。不过,我个人会改用:class_name 设置:

Issue with custom inflections in Ruby on Rails 3.0.3

【讨论】:

  • 您好,很抱歉撤回了我的接受,直到今天我才部分测试了您的解决方案。奇怪的是,它在控制台中有效,但在测试或我运行应用程序时无效。我已经根据新情况编辑了我的 OP。
  • 嗨..没问题。 spork 很可能没有加载变形文件。使用 spork -d 运行 spork 以查看它加载的内容,和/或将变形文件添加到 spec_helpers.rb 的 spork 预加载部分。还要制作一个仅测试变形的测试用例。看起来你需要一个! :)
  • 测试非常真实!我写了一些并通过测试再次编辑了 OP。测试通过了,但由于变形问题,我的应用仍然无法运行。
  • 抱歉这么久才再次接受。 Didin 不想在引入新功能的过程中更改我的表名。它最终与:class_name =&gt; 设置一起工作。非常感谢!
猜你喜欢
  • 2011-09-12
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
  • 2015-11-05
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
相关资源
最近更新 更多