私が参加したモデルは、私はいつでも私ので変曲問題があると思います。このレール3.1変曲問題
class ListingSave < ActiveRecord::Base
belongs_to :team
belongs_to :listing
end
モウのように見え、次の2機種
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
とのRails 3.1のアプリを持っていますテストを実行しようとすると、次のエラーが表示されます(これはエラーの例であり、それを引き起こしたテストです)
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
に次のように持っているように、私は以下の回答が続い 編集私は
this similar questionを見つけましたが、答えは
私の問題を解決していないようです
コンソールセッションを開いて"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インスタンス、同じファイルが読み込まれました。変わったことがここに起こっています!
# 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のドキュメント:
は、以下の私の新しいコメントを参照してください。 – Casper
愚かな質問かもしれませんが...もしそれがあなたにこれほど問題を与えているなら、それに別の名前を付けるのはなぜですか? ;) –
もちろん、私はそれが私を打つことはできません知っている! –