2

私はreason_to_sellというモデルを持っています。Rubyはそれをreason_to_sellsに複数化するので、これを追加しました。

ActiveSupport::Inflector.inflections do |inflect|
  inflect.plural 'reason_to_sell', 'reasons_to_sell'
end

これはコンソールでうまく機能します:

ruby-1.8.7-p302 > "reason_to_sell".pluralize
 => "reasons_to_sell"

販売する理由はそれぞれユーザーに帰属します。

class ReasonToSell < ActiveRecord::Base
  belongs_to :user

そしてもちろん、各ユーザーには販売する多くの理由があります。

class User < ActiveRecord::Base
  has_many :reasons_to_sell

しかし、これは私に与えます:

ruby-1.8.7-p302 > u.reasons_to_sell
NameError: uninitialized constant User::ReasonsToSell

しかし、販売する理由がたくさんあるようにユーザーを変更すると、状況は良くなります。

ruby-1.8.7-p302 > u=User.first ; u.reason_to_sells
 => [] 

では、reasons_to_sellの語尾変化をこのモデルの関連付けで機能させるには、何をする必要がありますか?

4

1 に答える 1

3

使用する:

has_many :reasons_to_sell, :class_name => "ReasonToSell"
于 2010-11-09T19:53:56.643 に答える