# Calling @books.photos.find(blah) vs getting record set first

**URL:** <https://www.ruby-forum.com/t/calling-books-photos-find-blah-vs-getting-record-set-first/80201>\
**Category:** Rails\
**Created:** [December 22, 2006, 2:54am UTC](https://www.ruby-forum.com/t/calling-books-photos-find-blah-vs-getting-record-set-first/80201 "2006-12-22T02:54:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [December 22, 2006, 2:54am UTC](https://www.ruby-forum.com/t/calling-books-photos-find-blah-vs-getting-record-set-first/80201/1 "2006-12-22T02:54:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [December 22, 2006, 8:42am UTC](https://www.ruby-forum.com/t/calling-books-photos-find-blah-vs-getting-record-set-first/80201/2 "2006-12-22T08:42:50Z")

</div>

On Dec 21, 6:54 pm, Aryk G. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [December 22, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/calling-books-photos-find-blah-vs-getting-record-set-first/80201/3 "2006-12-22T19:50:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [December 22, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/calling-books-photos-find-blah-vs-getting-record-set-first/80201/4 "2006-12-22T23:45:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [December 22, 2006, 9:21pm UTC](https://www.ruby-forum.com/t/calling-books-photos-find-blah-vs-getting-record-set-first/80201/5 "2006-12-22T21:21:15Z")

</div>

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
