Issue with custom inflections in Ruby on Rails 3.0

2020-05-04 12:35发布

I have a model called produccion_alternativa. I added a new inflection rule in config/initializers/inflections.rb, like this:

inflect.irregular('produccion_alternativa', 'producciones_alternativas')

I have other model called productor that has a relation with produccion_alternativa:

class Productor < ActiveRecord::Base
    has_many :producciones_alternativas


class ProduccionAlternativa < ActiveRecord::Base
    belongs_to :productor

When a I try to get all the producciones_alternativas for a productor, I get this error:

irb(main):010:0> Productor.first.producciones_alternativas
NameError: uninitialized constant Productor::ProduccionesAlternativa

Any ideas? Thank you very much

3条回答
Lonely孤独者°
2楼-- · 2020-05-04 12:44

I found another solution too. I added another rule on inflection.rb:

inflect.irregular('ProduccionAlternativa', 'ProduccionesAlternativas')
inflect.irregular('produccion_alternativa', 'producciones_alternativas')

At least, now it's working as I want. Thanx.

查看更多
唯我独甜
3楼-- · 2020-05-04 12:45

I see several others having the same problem. Couldn't find an answer why this happens. So in the meantime you could just try this:

has_many :producciones_alternativas, :class_name => "ProduccionAlternativa"
查看更多
姐就是有狂的资本
4楼-- · 2020-05-04 12:57

Your Fail is that you pluralized both words in has_many association. You used:

has_many :producciones_alternativas

but based on the class name ProduccionAlternativa the plural is produccion_alternativas because only the last word is pluralized! So this should work:

has_many :produccion_alternativas

To check the Plural of a word type "your_word".pluralize in the rails console!

查看更多
登录 后发表回答