Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5123 (closed defect: wontfix)

Opened 3 years ago

Last modified 2 years ago

[PATCH] bad pluralization on has_many

Reported by: andre@boaideia.inf.br Assigned to: David
Priority: normal Milestone: 1.2.4
Component: ActiveRecord Version: 1.1.1
Severity: normal Keywords: tiny fix activerecord reflection
Cc:

Description

ActiveRecord::Reflection::MacroReflection::AssociationReflection#name_to_class_name camelizes table name before singularize it on has_many associations, causing NameErrors exceptions.

There is a workaround, just fill :class_name with proper class name. but it's so ugly :-) anti-DRY too...

Attachments

fix_bad_pluralization_on_name_to_class_name.diff (3.6 kB) - added by andre@boaideia.inf.br on 05/18/06 19:39:44.
look that "the real patch" is 1 LOC long
bad_pluralization_clean_patch_hehe.diff (2.8 kB) - added by andre@boaideia.inf.br on 05/19/06 18:08:56.
more cleaner version ;-)
bad_pluralization_clean_patch_hehe.2.diff (2.6 kB) - added by andre@boaideia.inf.br on 05/19/06 18:13:53.
too bad... I almost forget my model…

Change History

05/18/06 19:39:44 changed by andre@boaideia.inf.br

  • attachment fix_bad_pluralization_on_name_to_class_name.diff added.

look that "the real patch" is 1 LOC long

05/18/06 20:59:36 changed by josh@hasmanythrough.com

Can you provide an example of this bug biting you? The associations, expect and actual behavior would be a good start.

05/18/06 23:00:53 changed by andre@boaideia.inf.br

of course I do!

the models: ===========

# Schema as of Thu May 18 19:27:06 BRT 2006 (schema version 63) # # id :integer not null # loja_id :integer # nome :string(25) # processo :integer default(1) #

class FormaDePagamento < ActiveRecord::Base

belongs_to :loja has_many :condicoes_de_pagamento validates_presence_of :nome

end

# Schema as of Thu May 18 19:27:06 BRT 2006 (schema version 63) # # id :integer not null # loja_id :integer # forma_de_pagamento_i:integer # parcelas :integer default(1) # entrada :integer default(1) # percentual :float default(0.0) #

class CondicaoDePagamento < ActiveRecord::Base

belongs_to :loja belongs_to :forma_de_pagamento validates_numericality_of :parcelas, :entrada, :percentual

end

I setup plurals in config/enviroment.rb =======================================

Inflector.inflections do |inflect|

inflect.irregular 'forma_de_pagamento', 'formas_de_pagamento' inflect.irregular 'condicao_de_pagamento', 'condicoes_de_pagamento'

end

OK, let's script/console now... ===============================

andre@dapper:~/rails/globalize$ ruby script/console Loading development environment.

forma1 = FormaDePagamento.find 1

=> #<FormaDePagamento:0xb7476980 @attributes={"processo"=>"1", "nome"=>"Depósito em Conta Corrente", "loja_id"=>"1", "id"=>"1"}>

forma1.condicoes_de_pagamento

NameError: /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:100:in `const_missing': uninitialized constant CondicoesDePagamento

from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1246:in `compute_type' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in `const_missing' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:133:in `const_missing' from (eval):1:in `compute_type' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/reflection.rb:112:in `klass' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations/has_many_association.rb:174:in `construct_sql' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations/has_many_association.rb:6:in `initialize' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations.rb:876:in `condicoes_de_pagamento' from (irb):2

below you see my environment: =============================

andre@dapper:~/rails/globalize$ ruby script/about About your application's environment Ruby version 1.8.4 (i486-linux) RubyGems version 0.8.11 Rails version 1.1.2 Active Record version 1.14.2 Action Pack version 1.12.1 Action Web Service version 1.1.2 Action Mailer version 1.2.1 Active Support version 1.3.1 Application root /home/andre/rails/globalize Environment development Database adapter sqlite3 Database schema version 63 Loaded suite script/about Started

Finished in 0.000553 seconds.

0 tests, 0 assertions, 0 failures, 0 errors

and here is the patch: ======================

Index: vendor/rails/activerecord/lib/active_record/reflection.rb =================================================================== --- vendor/rails/activerecord/lib/active_record/reflection.rb (revisão 4347) +++ vendor/rails/activerecord/lib/active_record/reflection.rb (cópia de trabalho) @@ -194,7 +194,7 @@

source_reflection.class_name

else

class_name = name.to_s.camelize

- class_name = class_name.singularize if [ :has_many, :has_and_belongs_to_many ].include?(macro) + class_name = name.to_s.singularize.camelize if [ :has_many, :has_and_belongs_to_many ].include?(macro)

class_name

end

end

05/18/06 23:08:13 changed by anonymous

Shame on me... I forgot WikiFormating.... :-(

the models:

# Schema as of Thu May 18 19:27:06 BRT 2006 (schema version 63)
#
#  id                  :integer       not null
#  loja_id             :integer       
#  nome                :string(25)    
#  processo            :integer       default(1)
#

class FormaDePagamento < ActiveRecord::Base
  belongs_to :loja
  has_many :condicoes_de_pagamento #, :class_name => 'CondicaoDePagamento'
  validates_presence_of :nome
end

# Schema as of Thu May 18 19:27:06 BRT 2006 (schema version 63)
#
#  id                  :integer       not null
#  loja_id             :integer       
#  forma_de_pagamento_i:integer       
#  parcelas            :integer       default(1)
#  entrada             :integer       default(1)
#  percentual          :float         default(0.0)
#

class CondicaoDePagamento < ActiveRecord::Base
  belongs_to :loja
  belongs_to :forma_de_pagamento
  validates_numericality_of :parcelas, :entrada, :percentual
end

I setup plurals in config/enviroment.rb

Inflector.inflections do |inflect|
  inflect.irregular 'forma_de_pagamento', 'formas_de_pagamento'
  inflect.irregular 'condicao_de_pagamento', 'condicoes_de_pagamento'
end

OK, let's script/console now...

andre@dapper:~/rails/globalize$ ruby script/console
Loading development environment.
>> forma1 = FormaDePagamento.find 1
=> #<FormaDePagamento:0xb7476980 @attributes={"processo"=>"1", "nome"=>"Depósito em Conta Corrente", "loja_id"=>"1", "id"=>"1"}>
>> forma1.condicoes_de_pagamento
NameError: /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:100:in `const_missing': uninitialized constant CondicoesDePagamento
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1246:in `compute_type'
        from /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in `const_missing'
        from /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:133:in `const_missing'
        from (eval):1:in `compute_type'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/reflection.rb:112:in `klass'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations/has_many_association.rb:174:in `construct_sql'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations/has_many_association.rb:6:in `initialize'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations.rb:876:in `condicoes_de_pagamento'
        from (irb):2

below you see my environment:

andre@dapper:~/rails/globalize$ ruby script/about
About your application's environment
Ruby version                 1.8.4 (i486-linux)
RubyGems version             0.8.11
Rails version                1.1.2
Active Record version        1.14.2
Action Pack version          1.12.1
Action Web Service version   1.1.2
Action Mailer version        1.2.1
Active Support version       1.3.1
Application root             /home/andre/rails/globalize
Environment                  development
Database adapter             sqlite3
Database schema version      63

and here is the patch:

Index: vendor/rails/activerecord/lib/active_record/reflection.rb
===================================================================
--- vendor/rails/activerecord/lib/active_record/reflection.rb   (revisão 4347)
+++ vendor/rails/activerecord/lib/active_record/reflection.rb   (cópia de trabalho)
@@ -194,7 +194,7 @@
               source_reflection.class_name
             else
               class_name = name.to_s.camelize
-              class_name = class_name.singularize if [ :has_many, :has_and_belongs_to_many ].include?(macro)
+              class_name = name.to_s.singularize.camelize if [ :has_many, :has_and_belongs_to_many ].include?(macro)
               class_name
             end
           end

05/19/06 18:08:56 changed by andre@boaideia.inf.br

  • attachment bad_pluralization_clean_patch_hehe.diff added.

more cleaner version ;-)

05/19/06 18:13:53 changed by andre@boaideia.inf.br

  • attachment bad_pluralization_clean_patch_hehe.2.diff added.

too bad... I almost forget my model...

05/23/07 19:16:50 changed by josh

  • status changed from new to closed.
  • resolution set to wontfix.