# Loading ActiveRecord associations by hand

**URL:** <https://www.ruby-forum.com/t/loading-activerecord-associations-by-hand/134283>\
**Category:** Rails\
**Created:** [April 9, 2008, 2:34pm UTC](https://www.ruby-forum.com/t/loading-activerecord-associations-by-hand/134283 "2008-04-09T14:34:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jurglic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jurglic](https://www.ruby-forum.com/u/jurglic)\
**Post date:** [April 9, 2008, 2:34pm UTC](https://www.ruby-forum.com/t/loading-activerecord-associations-by-hand/134283/1 "2008-04-09T14:34:22Z")

</div>

Hello,

I’m having this frustrating problem with AR. I want to load AR  
associations by hand. I have the following situation:

- 2 models: child and parent
- parent has has\_one association to child
- I have loaded set of parent objects (without children)

Now I want to load parent objects with childrens with only one sql  
select.

So I load child objects according to parent ids. Did it with one  
select, so that’s fine. But then the troubles begin. Now I want to  
dynamically associate (or fill association) every parent object with  
corresponding object. So I set it with:  
parent.child = child

But that also saves the object, which I do not want. I’ve also tried  
with “parent.build\_child( child.attributes”, but this is even worse,  
as it also destroys existing object before setting it…

So please… can anyone help me, how to load associations by hand?

Many thanks!

Uros

---

<div class="post-metadata">

**Author:** ![jurglic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jurglic](https://www.ruby-forum.com/u/jurglic)\
**Post date:** [April 9, 2008, 2:52pm UTC](https://www.ruby-forum.com/t/loading-activerecord-associations-by-hand/134283/2 "2008-04-09T14:52:48Z")

</div>

On 9 Apr 2008, at 13:33, removed\_email\_address@domain.invalid wrote:

> select.
> 
> So I load child objects according to parent ids. Did it with one  
> select, so that’s fine. But then the troubles begin. Now I want to  
> dynamically associate (or fill association) every parent object with  
> corresponding object. So I set it with:  
> parent.child = child

This is basically how edge rails handles eager loading, the relevant  
bit of code is at  
[http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active\_record/association\_preload.rb](http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/association_preload.rb)  
(look at the set\_association\_single\_records/  
set\_association\_collection\_records methods in particular, don’t forget  
to call loaded on the association too)

Fred

---

<div class="post-metadata">

**Author:** ![jurglic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jurglic](https://www.ruby-forum.com/u/jurglic)\
**Post date:** [April 9, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/loading-activerecord-associations-by-hand/134283/3 "2008-04-09T16:46:01Z")

</div>

Thanks for the tip Fred, it worked!

Instead of “parent.child = child”  
I’ve used: “parent.set\_child\_target( child )”

Work like a charm (so far)!
