This project is archived and is in readonly mode.

#6238 ✓invalid
Cristiano Ceglia

Problem with singular form of composed model names for has_many association in ActiveRecord

Reported by Cristiano Ceglia | December 31st, 2010 @ 08:57 AM

Hi,

I'm new in Rails so I hope to had not made mistakes.

I'm using Rails 3 and AR to build my own web app (in Italian) and I found a little problem when using has_many association and composed model names.

The example:

in config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'metodo_pagamento', 'metodi_pagamento' end

in app/models/cliente.rb
class Cliente < ActiveRecord::Base
has_many :metodi_pagamento, :dependent => :destroy end

in app/models/metodo_pagamento.rb
class MetodoPagamento < ActiveRecord::Base
belongs_to :cliente end

so when I run console
ruby-1.8.7-p302 > Cliente.new.metodi_pagamento
I get this
NameError: uninitialized constant Cliente::MetodiPagamento <======== ( plural form instad singular form )

from /home/cristiano/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-3.0.3/lib/active_record/base.rb:1199:in `compute_type'
from /home/cristiano/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-3.0.3/lib/active_record/reflection.rb:162:in `send'
from /home/cristiano/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-3.0.3/lib/active_record/reflection.rb:162:in `klass'
from /home/cristiano/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-3.0.3/lib/active_record/reflection.rb:198:in `quoted_table_name'
from /home/cristiano/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-3.0.3/lib/active_record/associations/has_many_association.rb:102:in `construct_sql'
from /home/cristiano/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:24:in `initialize'
from /home/cristiano/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-3.0.3/lib/active_record/associations/has_many_association.rb:11:in `initialize'
from /home/cristiano/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-3.0.3/lib/active_record/associations.rb:1492:in `new'
from /home/cristiano/.rvm/gems/ruby-1.8.7-p302/gems/activerecord-3.0.3/lib/active_record/associations.rb:1492:in `metodi_pagamento'
from (irb):2

So, AR doesn't translate the has_many association :metodi_pagamento in the class MetodoPagamento, but it translate it in MetodiPagamento (which is the plural form of MetodoPagamento).

I searched in AR sources and I found the function derive_class_name in gems/activerecord-3.0.3/lib/active_record/reflection.rb:
def derive_class_name
class_name = name.to_s.camelize class_name = class_name.singularize if collection? class_name end

This function try to get the singular form of the model from Calized form, but in inflections aren't we supposed to write plural and singular form in underscore form?
So I managed to adjast the function like so:
def derive_class_name
class_name = name.to_s class_name = class_name.singularize if collection? class_name.camelize end

After this it ran smoothly.
Sure, I could use the :class_name option in model, but...

Punch me if I made mistakes, please.

Thank you

Comments and changes to this ticket

  • Andrew White

    Andrew White December 31st, 2010 @ 10:10 AM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”

    Cristiano, you should try and make your inflections work irrespective of whatever form it takes as there are other places where the name may not be in underscored form (e.g it may have been humanized). Using your example you can use the regular plural and singular inflection methods like this:

    ActiveSupport::Inflector.inflections do |inflect|
      inflect.plural /^(m)etodo(.+)?$/i, '\1etodi\2'
      inflect.singular /^(m)etodi(.+)?$/i, '\1etodo\2'
    end
    

    My italian is very basic but I'm assuming that combining the word method with any other word follows the same rule - if not you may need to tweak the regexps.

  • bingbing

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages