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