Finding records based on different fields/columns?

21,646

Solution 1

Use

User.find_by_email("[email protected]")

Must check these two post from railscast (For rails3)

http://railscasts.com/episodes/202-active-record-queries-in-rails-3

http://railscasts.com/episodes/215-advanced-queries-in-rails-3

Solution 2

You can also query

User.where("email = ?", "[email protected]").first
Share:
21,646
Wasi
Author by

Wasi

Computer engineering student at University of Toronto

Updated on October 23, 2020

Comments

  • Wasi
    Wasi over 3 years

    I have a database that has stored information of some users. I know for example: User.find(1) will return the user with id:1

    What should I call to find a user by email? I searched a lot but could not find anything.

    I also tried User.find(:email => "[email protected]") but it doesn't work.

  • Rik Poggi
    Rik Poggi over 11 years
    Just to add something, there's also a rails guide on queries. And here is shown that the find_by_* is dynamically available.
  • RJHunter
    RJHunter over 7 years
    Welcome to Stack Overflow! It sounds like you have a tip you want to share, and that's great, but it seems like it should be on a different question. It doesn't seem like an "answer" that matches this question. Stack Overflow tries to keep a quite tight match between questions and answers. You can even ask your own question and answer it yourself if you like.
  • user229044
    user229044 over 6 years
    This is a misuse of ActiveRecord. You should use User.where(email: '[email protected]').