次の2つのモデルを備えたRails3.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
結合モデルは次のようになります
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」が表示されます。しかし、私のアプリケーションの少なくとも一部がそれを取得していないようです。私のテストは以前と同じように失敗します。(アプリケーションをテスト/実行する前に、サーバーとスポークを再起動することを誓います!)
編集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インスタンス、同じファイルが読み込まれます。ここで何か変なことが起こっています!??