has_one :through NoMethodError: undefined method `klass' for nil:NilClass when doing class_name on reflection

Not sure if this is expected or not. Only happened with a has_one :through I had setup. (Workaround is just to remove it and go through the association manually vs has_one …, through: .)

Not a big deal if no one has time to look at it, as it’s not a big enough deal to look into a fix on our side.

In Rails (3.2.9) console if I do:

MyModel.reflections.each {|name,reflection| reflection.class_name }

it raises:

NoMethodError: undefined method `klass’ for nil:NilClass

from /path/to/rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.9/lib/active_record/reflection.rb:385:in block in source_reflection' from /path/to/rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.9/lib/active_record/reflection.rb:385:in collect’

from /path/to/rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.9/lib/active_record/reflection.rb:385:in source_reflection' from /path/to/rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.9/lib/active_record/reflection.rb:530:in derive_class_name’ from /path/to/rvm/gems/ruby-1.9.3-p194@my_project/gems/activerecord-3.2.9/lib/active_record/reflection.rb:127:in class_name' from (irb):6:in block in irb_binding’ from (irb):6:in each' from (irb):6 from /path/to/rvm/gems/ruby-1.9.3-p194@my_project/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in start’ from /path/to/rvm/gems/ruby-1.9.3-p194@my_project/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in start' from /path/to/rvm/gems/ruby-1.9.3-p194@my_project/gems/railties-3.2.9/lib/rails/commands.rb:41:in <top (required)>’ from script/rails:6:in require' from script/rails:6:in

Simplified versions of models:

class MyModel < ActiveRecord::Base

has_one :bar, through: :foo

end

class Foo < ActiveRecord::Base

self.table_name = :somenonstandardnamehere self.primary_key = :nonstandardish_id

has_one :bar, foreign_key: ‘anothernonstandard_id’

end