我有一个带有以下 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
Mow 我认为存在拐点问题,因为每当我尝试运行我的测试时,我都会收到以下错误(这是一个错误示例和导致它的测试)
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
接下来我能做什么?
注意:我发现了这个类似的问题,但答案似乎没有解决我的问题
编辑
我遵循了下面的答案,所以我现在在我的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 实例,加载相同的文件。这里发生了什么奇怪的事情!???