Actually, if you did this:

Posts.all.each{ |post| post.account.name }

that's just one query.  Collections are lazily loaded, so unless you
actually call the results of the collection, it doesn't make a query.  So
Posts.all doesn't make a DB call.  post.account.name will though.

It's hard to tell in IRB because irb calls to_s on everything.  So even the
code you have right there, when run inside a ruby script should only make
one db call.

(unless i'm horribly confused, which i don't believe i am!)

-T

On Mon, Apr 5, 2010 at 10:47 PM, Tiago Martines <[email protected]> wrote:

> Hi all,
>
> I want to build a simple join in an association:
>
> (collapsed)
>
> def Account
>    (if not needed, no has n, :posts here!)
> end
>
> def Post
>    belongs_to :account
> end
>
> posts = Post.all = 1 query
>
> posts.each do |post|
>    post.account.name = more 1 query
> end
>
> I think if is possible to make this example build only 1 query instead
> 2.
>
> Thank you guys!
>
> --
> You received this message because you are subscribed to the Google Groups
> "DataMapper" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<datamapper%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/datamapper?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to