Find_by_xxx incorrectly adds LIMIT 1

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs