1 User -> many Accounts
If I eager load the user with: my_user = User.find(:first, include =>
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 =>
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
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
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.