.find by

suggest change

You can find records by any field in your table using find_by.

So, if you have a User model with a first_name attribute you can do:

User.find_by(first_name: "John")
#=> #<User id: 2005, first_name: "John", last_name: "Smith">

Mind that find_by doesn’t throw any exception by default. If the result is an empty set, it returns nil instead of find.

If the exception is needed may use find_by! that raises an ActiveRecord::RecordNotFound error like find.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


ActiveRecord Query Interface:
*.find by

Table Of Contents
12ActiveRecord Query Interface
55CSV