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开发者_JS百科 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 => "xyz@abc.com")
but it doesn't work.
Use
User.find_by_email("abc@xyz.com")
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
You can also query
User.where("email = ?", "abc@xyz.com").first
By any chance you just registered a user and what to find this specific user and retrieve all the user data through the instance,
- You want to store it in a session[:current_user] = @user
- @user = User.find_by_email(params[:email])
- You want to do this in your create method in your users controller.
- Now you have access to this new user instance which will be helpful for times when you want them to have a profile page ready for them.
- Hope it wasn't too off topic, and if so, hope this helps with your future projects! Good Luck!
精彩评论