rails 3.1拐点问题 [英] rails 3.1 inflection problem
问题描述
我有一个具有以下2种型号的Rails 3.1应用程序
I have a Rails 3.1 app with the following 2 models
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模型看起来像这样
The Join model looks like this
class ListingSave < ActiveRecord::Base
belongs_to :team
belongs_to :listing
end
现在我认为这是一个曲折问题,因为每当我尝试运行测试时,都会收到以下错误(这是一个错误示例,也是导致该错误的示例)
Mow I think that there is an inflection problem because whenever I try to run my tests I get the following error (this is an example of an error and the test that caused it)
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
as if Rails is singularizing listing_saves
into listing_safe
以下是我尝试过的一些自定义变形器(并非同时使用)(都不起作用)
Here are some custom inflectors I have tried (not all at the same time) (none of them work)
# 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
接下来我该怎么办?
注意:我发现了类似的问题,但答案似乎没有解决我的问题
Note: I found the this similar question but the answer doesn't seem to solve my problem
修改
我遵循以下答案,因此我的config/initializers/inflections.rb
Edit
I followed the answer below so that I now have the following in my config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'listing_save', 'listing_saves'
end
当我打开一个控制台会话并运行"listing saves".singularize
时,正如我希望的那样,我得到了"listing_save".但是,似乎至少我的应用程序的一部分没有得到它,我的测试仍然以与以前相同的方式失败. (我发誓要在测试/运行该应用程序之前重新启动服务器并启动它!).
When I open up a console session and run "listing saves".singularize
I get "listing_save" as I would hope. However, it seems that at least part of my application doesn't get it, my tests still fail in the same way as before. (I swear I'm restarting my server and spork before I test/run the application!).
编辑2 我在我的应用程序中编写了一些关于拐点的测试:
Edit 2 I wrote some tests for inflections in my app:
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
现在,我的情况是这些测试可以顺利通过,但其他测试仍然失败,并出现了与以前相同的错误
Now I have a situation where these tests pass fine, but other tests still fail with the same error I was having before
NameError:
uninitialized constant User::ListingSafe
相同的应用程序,相同的spork实例,相同的文件已加载.
Same app, same spork instance, same files loaded. Something weird is going on here!??
推荐答案
您需要定义不规则拐点:
# 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'
一些进一步的调查-似乎其他人也偶然发现了这个相同的问题(在关联方面,拐点不起作用).因此,与此同时,您可以手动设置类名称:
Some further investigation - and it looks like others have stumbled upon this same problem also (inflections not working as expected with associations). So in the meantime you can set the class name manually:
has_many :listing_saves, :class_name => "ListingSave"
其他人也有相同的问题,并且进行了其他的调整.就我个人而言,我会改用:class_name
设置:
Someone else with the same problem, and an additional inflections tweak. Personally I'd go with the :class_name
setting instead though:
这篇关于rails 3.1拐点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!