简体   繁体   中英

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

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?

您应该使用where ,就像这样

self.where(name: name, day: day, user_id: user_id)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM