# Find\_by\_xxx incorrectly adds LIMIT 1

**URL:** <https://www.ruby-forum.com/t/find-by-xxx-incorrectly-adds-limit-1/105378>\
**Category:** Rails\
**Created:** [May 29, 2007, 1:34pm UTC](https://www.ruby-forum.com/t/find-by-xxx-incorrectly-adds-limit-1/105378 "2007-05-29T13:34:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gioele](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8491ac/32.png) [@gioele](https://www.ruby-forum.com/u/gioele)\
**Post date:** [May 29, 2007, 1:34pm UTC](https://www.ruby-forum.com/t/find-by-xxx-incorrectly-adds-limit-1/105378/1 "2007-05-29T13:34:57Z")

</div>

I have Products and Reviews; Product :has\_many reviews, Review  
:belongs\_to product.

Review.find\_by\_product\_id(5) returns only one Review because it is  
incorrectly translated into (MySQL) “SELECT \* FROM reviews WHERE  
(reviews.`product_id` = 5) LIMIT 1”. So

> > Product.find(5).reviews.count  
> > =\> 6

> > Review.find\_by\_product\_id(5)  
> > =\> #\<Review:0x2b1cefb2a1d8 @attributes=…\>

How can I tell Rails that Review.find\_by\_product\_id should return a  
collection and not a single result?

–  
Gioele

---

<div class="post-metadata">

**Author:** ![gioele](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8491ac/32.png) [@gioele](https://www.ruby-forum.com/u/gioele)\
**Post date:** [May 29, 2007, 1:42pm UTC](https://www.ruby-forum.com/t/find-by-xxx-incorrectly-adds-limit-1/105378/2 "2007-05-29T13:42:54Z")

</div>

Gioele B. wrote:

> How can I tell Rails that Review.find\_by\_product\_id should return a  
> collection and not a single result?  
> Answering to myself: use Review.find\_all\_by\_product\_id instead.

–  
Gioele

---

<div class="post-metadata">

**Author:** ![gioele](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8491ac/32.png) [@gioele](https://www.ruby-forum.com/u/gioele)\
**Post date:** [May 30, 2007, 4:36am UTC](https://www.ruby-forum.com/t/find-by-xxx-incorrectly-adds-limit-1/105378/3 "2007-05-30T04:36:13Z")

</div>

Hi Gioele,

Gioele B. wrote:

> How can I tell Rails that Review.find\_by\_product\_id should return a  
> collection and not a single result?

Review.find\_all\_by\_product\_id(5)

If you wanted to limit the result set you’d need to move to the  
:conditions  
=\> [,] syntax and use :limit

hth,  
Bill
