3

次の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インスタンス、同じファイルが読み込まれます。ここで何か変なことが起こっています!??

4

1 に答える 1

11

不規則な語尾変化を定義する必要があります。

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

編集:

いくつかのさらなる調査-そして他の人もこの同じ問題に遭遇したようです(語尾変化は協会で期待どおりに機能していません)。その間、クラス名を手動で設定できます。

has_many :listing_saves, :class_name => "ListingSave"

同じ問題を抱えている他の誰かが、追加の語尾変化を微調整します。個人的には、:class_name代わりに設定を使用します。

Ruby onRails3.0.3のカスタム屈折に関する問題

于 2011-07-06T23:34:17.387 に答える