Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding records based on different fields/columns?

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.

like image 778
Wasi Avatar asked May 15 '11 15:05

Wasi


2 Answers

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

like image 190
Mohit Jain Avatar answered Oct 23 '22 12:10

Mohit Jain


You can also query

User.where("email = ?", "[email protected]").first
like image 20
Sergey Kishenin Avatar answered Oct 23 '22 10:10

Sergey Kishenin