# After loading model, can it be again accessed without SQL

**URL:** <https://www.ruby-forum.com/t/after-loading-model-can-it-be-again-accessed-without-sql/92154>\
**Category:** Rails\
**Created:** [April 16, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/after-loading-model-can-it-be-again-accessed-without-sql/92154 "2007-04-16T18:07:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercurykjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@mercurykjb](https://www.ruby-forum.com/u/mercurykjb)\
**Post date:** [April 16, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/after-loading-model-can-it-be-again-accessed-without-sql/92154/1 "2007-04-16T18:07:31Z")

</div>

Quick question:

1 User -\> many Accounts

If I eager load the user with: my\_user = User.find(:first, include =\>  
[:accounts])

How can I then use criteria to work with the “accounts” without making  
another query to the database?

Iteration is one option, but it would be best to avoid it if say I  
wanted one of the accounts in the user by :name =\> “Checking Account”.

Thanks a lot of any advice.

Kendall B.

---

<div class="post-metadata">

**Author:** ![mercurykjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@mercurykjb](https://www.ruby-forum.com/u/mercurykjb)\
**Post date:** [April 16, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/after-loading-model-can-it-be-again-accessed-without-sql/92154/2 "2007-04-16T18:43:12Z")

</div>

> Iteration is one option, but it would be best to avoid it if  
> say I wanted one of the accounts in the user by :name =\>  
> “Checking Account”.

I don’t think you can. However, you might be able to do it another way.  
If you know before the load which one account you want to look at, you  
could do:

User.find(:first, :include=\>[:accounts], :conditions=\>[‘accounts.name =  
?’, ‘Checking Account’])

perhaps a bit less efficient than including the condition in the JOIN  
itself, but it ought to work. Note that this will only work properly if  
you’re only :including one relationship with many entities.

An alternate solution might be to include the condition in a manual  
join:

User.find(:first, :joins=\>'LEFT OUTER JOIN accounts on accounts.user\_id  
= users.id AND accounts.name = ‘Checking Account’)

then the account properties will appear on the user object, but I dunno  
what will happen if the user and account objects share any property  
names, nor do I know if the automatic sql type-\>ruby type mappings will  
occur as usual.

- donald

---

<div class="post-metadata">

**Author:** ![mercurykjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@mercurykjb](https://www.ruby-forum.com/u/mercurykjb)\
**Post date:** [April 16, 2007, 11:45pm UTC](https://www.ruby-forum.com/t/after-loading-model-can-it-be-again-accessed-without-sql/92154/3 "2007-04-16T23:45:49Z")

</div>

from what i know that’s no problem at all ?!? that’s the whole point  
of eager loading…

@user = User.find(:first, include =\> [:accounts])

@user.accounts.find(blablaba-)  
@user.accounts.each do { |account| blablabla }  
@user.accounts.count

these and more will workwithout re-querying the database. basically  
any AR method you use on @user.accounts won’t hit the database, except  
for updating/adding/removing an account, of course.

On 16 Apr., 18:35, “Ball, Donald A Jr (Library)”

---

<div class="post-metadata">

**Author:** ![mercurykjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@mercurykjb](https://www.ruby-forum.com/u/mercurykjb)\
**Post date:** [April 17, 2007, 12:08am UTC](https://www.ruby-forum.com/t/after-loading-model-can-it-be-again-accessed-without-sql/92154/4 "2007-04-17T00:08:53Z")

</div>

> the database, except for updating/adding/removing an account,  
> of course.

My understand was that he was trying to query the eagerly loaded user’s  
accounts using SQL conditions, which AFAIK you cannot do without  
requerying the database. Sure, you can iterate through and find the  
account named ‘Checking Account’, but it’s probably faster to have the  
database do the work for you.

For a handful of accounts and a simple criteria like this, it’s almost  
certainly 6 == 1/2 dozen and I’d probably do it all in ruby for clarity,  
but if there are many children and complex criteria, I’d do it in SQL.

- donald
