# Find\_by\_sql not settings model attributes and eager loading

**URL:** <https://www.ruby-forum.com/t/find-by-sql-not-settings-model-attributes-and-eager-loading/128435>\
**Category:** Rails\
**Created:** [February 7, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/find-by-sql-not-settings-model-attributes-and-eager-loading/128435 "2008-02-07T18:05:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 7, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/find-by-sql-not-settings-model-attributes-and-eager-loading/128435/1 "2008-02-07T18:05:18Z")

</div>

Hi all,

I want to get all users for an event and the registration data:  
I use find\_by\_sql to get data for 2 models: users and registrations  
(user has many events through registrations).

@players = User.find\_by\_sql([“SELECT users._, registrations._ FROM  
users LEFT JOIN registrations ON users.id = registrations.user\_id AND  
registrations.event\_id = ?”, @event.id.to\_s])

It is intended that when no registration is available I still get the  
user information. When no registration record is available though the  
the user.id attribute is nil in the user objects. This only happenes  
when no record for that user and event is available.

Someone knows why?

Also, can I somehow tell rails to fill the registrations data in the  
registrations object (like the eager loading :include in find) under  
the user object? Now, it all gets filled in the user object  
(registration.present for example is in the user model!)

Thanks  
Stijn

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 7, 2008, 7:02pm UTC](https://www.ruby-forum.com/t/find-by-sql-not-settings-model-attributes-and-eager-loading/128435/2 "2008-02-07T19:02:43Z")

</div>

On 7 Feb 2008, at 17:03, Tarscher wrote:

> It is intended that when no registration is available I still get the  
> user information. When no registration record is available though the  
> the user.id attribute is nil in the user objects. This only happenes  
> when no record for that user and event is available.
> 
> Someone knows why?

Because you need to alias your columns.  
The result set will have a users.id column (not null) and a  
registrations.id column (which is null). AR is just looking at the  
column name and so sod’s law means that the null id clobbers the not  
null id (and in other cases players.id could easily be the  
registration.id not the users.id)

Fred

> Also, can I somehow tell rails to fill the registrations data in the  
> registrations object (like the eager loading :include in find) under  
> the user object? Now, it all gets filled in the user object  
> (registration.present for example is in the user model!)  
> find\_by\_sql doesn’t know how to do that. There is a plugin that allows  
> you to write custom sql with eager loading  
> ([http://kellogg-assoc.com/articles/2006/11/05/eager-finder-sql](http://kellogg-assoc.com/articles/2006/11/05/eager-finder-sql)  
> )
