derive_class_name not singularizing has_many relation name #1888

Closed
tispratik opened this Issue Jun 28, 2011 · 1 comment

Projects

None yet

1 participant

@tispratik
Tested on Rails Version 3.0.3 and 3.0.4

class Quote < ActiveRecord::Base
  has_many :options
end

class Option < ActiveRecord::Base
  belongs_to :quote
end

ruby-1.9.2-p180 :002 > Quote.first.options
NameError: uninitialized constant Quote::Options
    from /Users/rack/.rvm/gems/ruby-1.9.2-p180@rails3/bundler/gems/rails/activerecord/lib/active_record/base.rb:1199:in `compute_type'
    from /Users/rack/.rvm/gems/ruby-1.9.2-p180@rails3/bundler/gems/rails/activerecord/lib/active_record/reflection.rb:162:in `klass'
    from /Users/rack/.rvm/gems/ruby-1.9.2-p180@rails3/bundler/gems/rails/activerecord/lib/active_record/reflection.rb:198:in `quoted_table_name'
    from /Users/rack/.rvm/gems/ruby-1.9.2-p180@rails3/bundler/gems/rails/activerecord/lib/active_record/associations/has_many_association.rb:102:in `construct_sql'
    from /Users/rack/.rvm/gems/ruby-1.9.2-p180@rails3/bundler/gems/rails/activerecord/lib/active_record/associations/association_collection.rb:24:in `initialize'
    from /Users/rack/.rvm/gems/ruby-1.9.2-p180@rails3/bundler/gems/rails/activerecord/lib/active_record/associations/has_many_association.rb:11:in `initialize'
    from /Users/rack/.rvm/gems/ruby-1.9.2-p180@rails3/bundler/gems/rails/activerecord/lib/active_record/associations.rb:1492:in `new'
    from /Users/rack/.rvm/gems/ruby-1.9.2-p180@rails3/bundler/gems/rails/activerecord/lib/active_record/associations.rb:1492:in `block in collection_reader_method'
    from (irb):2
    from /Users/rack/.rvm/gems/ruby-1.9.2-p180@rails3/bundler/gems/rails/railties/lib/rails/commands/console.rb:44:in `start'
    from /Users/rack/.rvm/gems/ruby-1.9.2-p180@rails3/bundler/gems/rails/railties/lib/rails/commands/console.rb:8:in `start'
    from /Users/rack/.rvm/gems/ruby-1.9.2-p180@rails3/bundler/gems/rails/railties/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
ruby-1.9.2-p180 :003 >

After debugging, i realized that the method derive_class_name from class activerecord/lib/active_record/reflection.rb is not singularizing the association name before calling to_s.camelize on the name.

Also, when i explicitly declare the class name 'Option' in the model, it works. So, the following works.

class Quote < ActiveRecord::Base
  has_many :options, :class_name => "Option"
end
@tispratik

Found it. It was due to a monkey patch in thoughtworks_commons gem. It worked after commenting it out. Closing this issue.

module ActiveRecord
  module Reflection
    class AssociationReflection < MacroReflection
      def initialize(macro, name, options, active_record)
        super(macro, name, options, active_record)
        if active_record && name
          active_record.send(:define_method, "has_#{name}?") { !self.send(name).blank? }
          active_record.send(:alias_method, "has_#{name}", "has_#{name}?")
        end
      end
    end
  end
end
@tispratik tispratik closed this Jun 29, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment