3

私が参加したモデルは、私はいつでも私ので変曲問題があると思います。このレール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のドキュメント:

+0

は、以下の私の新しいコメントを参照してください。 – Casper

+0

愚かな質問かもしれませんが...もしそれがあなたにこれほど問題を与えているなら、それに別の名前を付けるのはなぜですか? ;) –

+0

もちろん、私はそれが私を打つことはできません知っている! –

答えて

11

あなたが不規則抑揚を定義する必要が

------------------------ 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

+0

偉大な答え、ありがとう! –

+0

こんにちは、私の受諾を取り消して申し訳ありません、私はあなたのソリューションを今日まで部分的にしかテストしていませんでした。奇妙なことに、コンソールでは動作しますが、テストでは動作しません。新しい状況でOPを編集しました。 –

+0

こんにちは..問題ありません。 sporkがinflectionsファイルをロードしていない可能性があります。 'spork -d'を使ってsporkを実行して、読み込んだ内容を調べるか、またはinflectionsファイルを' spec_helpers.rb'のspork preloadセクションに追加します。また、変曲点だけをテストするテストケースを作成します。あなたはそれが必要なようですね! :) – Casper