开发者

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,

  1. You want to store it in a session[:current_user] = @user
  2. @user = User.find_by_email(params[:email])
  3. You want to do this in your create method in your users controller.
  4. 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.
  5. Hope it wasn't too off topic, and if so, hope this helps with your future projects! Good Luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜