Home In rails, why is my finder method only returning one result when I want it to return all of them?
Reply: 2

In rails, why is my finder method only returning one result when I want it to return all of them?

Dave
1#
Dave Published in 2017-11-08 15:27:23Z

I'm using Rails 5.0.3. How do I find all matching records using a finder? I have

my_obj = self.find_by_name_and_day_and_user_id(name, day, user_id)

but it returns only a single result. When I run turn on the SQL, it is adding a

 LIMIT 1

clause. How do I write a finder method that will return all results and not just one?

Nikita Misharin
2#
Nikita Misharin Reply to 2017-11-08 15:30:13Z

You should use where, like so

self.where(name: name, day: day, user_id: user_id)
Junan Chakma
3#
Junan Chakma Reply to 2017-11-08 15:38:15Z

find_by method always return first matching record. So you should use where clause which will return all matching records.

my_obj = self.where(name: name, day: day, user_id: user_id)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.354101 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO