Help with Eager Association


#1

I’ve gotten eager associations to work before, but for some reason
this isn’t working out for me this time. I’ve been scratching my head
but can’t quite figure out the nuances of what works and what doesn’t
work.

I’ve got 3 models: profile, widget_configuration and widgets

profile.rb –
has_many :widget_configurations

widget_configuration.rb -
belongs_to :profile
belongs_to :widgets

widgets.rb -
has_many :widget_configurations

So in otherwords, widget_configuration is a sort of ‘rich’ m-to-m
association table containing foreign keys to both profiles and
widgets. The table looks like this:
widget_configurations

profile_id (FK)
widget_id (FK)


So now what I’m trying to do is this:
(assuming @profile is an instance variable w/ a profile)
@profile.widget_configurations.find :all, :include => [:widget]

I end up getting this error:
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.first

This shows up in the log file:

WidgetConfiguration Columns (0.000256) SHOW FIELDS FROM
widget_configurations
Widget Columns (0.000252) SHOW FIELDS FROM widgets

NoMethodError (You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.first):
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:924:in
generate_primary_key_table' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:849:infind_with_associations’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:395:in
find' /app/controllers/home_controller.rb:10:inindex’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in
send' /app/controllers/home_controller.rb:10:inindex’
… etc… the rest traces back to the dispatch.cfgi…

I’m about this close to just doing a find_by_sql and handling the
association myself, but I’d rather do it using the built-in
mechanisms.

Any help?