Hi guys,

I came across this obscure bug in the new preload associations:

NoMethodError (undefined method `preload_associations' for
NilClass:Class):
    /vendor/rails/activerecord/lib/active_record/
association_preload.rb:25:in `preload_associations'
    /vendor/rails/activerecord/lib/active_record/
association_preload.rb:19:in `each'
    /vendor/rails/activerecord/lib/active_record/
association_preload.rb:19:in `preload_associations'
    /vendor/rails/activerecord/lib/active_record/
association_preload.rb:16:in `preload_associations'
    /vendor/rails/activerecord/lib/active_record/
association_preload.rb:16:in `each'
    /vendor/rails/activerecord/lib/active_record/
association_preload.rb:16:in `preload_associations'
    /vendor/rails/activerecord/lib/active_record/base.rb:1247:in
`find_every'
    /vendor/rails/activerecord/lib/active_record/base.rb:502:in `find'
....

This occurs when I pass a two parameters in an :order statement, at
least this is the only way I've come across the issue and been able to
replicate it.

HumanResource::Person.find(:all, :include => [:person_position
=> :position], :order => 'last_name')
-- works fine

HumanResource::Person.find(:all, :include => [:person_position
=> :position], :order => 'last_name,first_name')
-- causes error above

However, there is more to the problem, I don't believe it revolves
around the :order parameter after looking at the
association_preload.rb code. When I step through the code on line 23:
parents = records.map {|record| record.send(reflection.name)}.flatten

parents will return [nil, #<HumanResource::Person::Position id: 2,
person_id: 10001, position_id: 4, void_cheque: 0, hired_on: nil,
terminated_on: nil>]

The issue is when the preload of the association takes place, one of
the models returns nothing, the first element, and this creates an
error on line 25. To fix this, I simply added ".compact" to the end of
line 23. Unfortunately I don't have experience messing around with the
Rails core or writing unit tests for it so I wasn't able to formalize
this issue.

Regards,

Peter
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to