Forum: Ruby on Rails calling @books.photos.find(blah) vs getting record set first

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eric G. (Guest)
on 2006-12-22 03:54
Which one is better?

@book.photos.count(:conditions=>["uploader_id=?",@c.id])
@book.photos.find(:all,:conditions=>["uploader_id=?",@c.id],:offset=>@offset,:limit=>@limit)

OR

book = @book.photos
book.count(:conditions=>["uploader_id=?",@c.id])
book.find(:all,:conditions=>["uploader_id=?",@c.id],:offset=>@offset,:limit=>@limit)


I heard that when you do @book.photo.find(blah), it actually does ONE
query by combining them, same thing going for the count which I believe
use count(*).

So I think the first one is better... any thoughts?
Aaron (Guest)
on 2006-12-22 09:42
(Received via mailing list)
On Dec 21, 6:54 pm, Aryk G. <removed_email_address@domain.invalid>
wrote:
> Which one is better?
>
> @book.photos.count(:conditions=>["uploader_id=?",@c.id])
> 
@book.photos.find(:all,:conditions=>["uploader_id=?",@c.id],:offset=>@offset,:limit=>@limit)
>
> OR
>
> book = @book.photos
> book.count(:conditions=>["uploader_id=?",@c.id])
> book.find(:all,:conditions=>["uploader_id=?",@c.id],:offset=>@offset,:limit=>@limit)

Is this a trick question?  ;-)  They are exactly the same

book = @book.photos
book.object_id == @book.photos.object_id  =>  true

Aaron
Eric G. (Guest)
on 2006-12-22 20:50
Aaron, somewhere I heard that when you do @book.photos.find(blah), it
actually does @book.find(:all,:conditions=>"photo_id=blah").find(blah)
and then COMBINES the two queries into one, effectively doing just one
query.


However if i do book=@book.photos I do a query. Then if I do
book.find(blah) and then Im doing two queries.

Where am I going wrong with this?
Mark D. (Guest)
on 2006-12-22 22:21
Aryk,

The first one is quicker, as it only performs one query.

'@site.pages.count' results in this query:

SELECT count(*) AS count_all FROM pages WHERE (pages.site_id = X)

The second one performs two queries, and the first may return lots of
records which you do not need.

'pages = @sites.pages; pages.count' result in two queries:

SELECT * FROM pages WHERE (pages.site_id = X)
SELECT count(*) AS count_all FROM pages WHERE (pages.site_id = X)

Of course, this depends on whether @site has already had it's 'pages'
attribute loaded. For each instance of the Site class, once the 'pages'
attribute has been loaded from the database it will stay in memory and
so subsequent calls to '@site.pages' will not result in a db hit.

Cheers,

Mark

PS. X = @site.id
Eric G. (Guest)
on 2006-12-23 00:45
Hey Mark, thanks for the response...

>>Of course, this depends on whether @site has already had it's 'pages'
>>attribute loaded. For each instance of the Site class, once the 'pages'
>>attribute has been loaded from the database it will stay in memory and
>>so subsequent calls to '@site.pages' will not result in a db hit.

When you say already had its pages loaded, are you saying that somewhere
earlier in the action, @site.pages was called, so it was already cached
for that method. I didnt know RoR could do that.
This topic is locked and can not be replied to.