Eager loading has_many through

hi all,

My relationships:

  • group habtm user and
  • user has_many events :through statistics.
    The statistics contains the id of user and is of event and additional
    data.

How can I get all the users in a group with their statistics via eager
loading.

I tried:
@users = a_node.users.find(:all, :include => :statistics, :conditions
=> [“statistics.event_id = ?”, event.id])

The problem is that I only get the users with a statistics record and
I also want users without any stastics. I think the problem is caused
by :conditions => [“statistics.event_id = ?”, event.id] but I don’t
know how to include the event_id in another way.

Someone can help me out?

Thanks
-Stijn

The problem is that I only get the users with a statistics record and
I also want users without any stastics. I think the problem is caused
by :conditions => [“statistics.event_id = ?”, event.id] but I don’t
know how to include the event_id in another way.

Someone can help me out?

Thanks
-Stijn

Try using the :joins parameter and manually join to the statistics table
with an OUTER join, or use the find_by_sql method if you have to.

The joins param value will be something like :joins => “left outer join
statistics s on s.user_id = user.id”

Having the left outer join should still allow you to filter the the
statistics with the event_id and include the users with and without
stats.