开发者

has_many association does not respect custom inflector

I have a model called reason_to_sell. Ruby will plurali开发者_如何学Pythonze that to reason_to_sells, so I added this:

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

This works great in the console:

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

Each reason to sell belongs to a user:

class ReasonToSell < ActiveRecord::Base
  belongs_to :user

And of course each user can have many reasons to sell:

class User < ActiveRecord::Base
  has_many :reasons_to_sell

However, this gives me:

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

But if I change the user to have many reason to sells, things get better:

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

So what do I need to do in order to get the reasons_to_sell inflection to work on this model association?


Use:

has_many :reasons_to_sell, :class_name => "ReasonToSell"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜