开发者

Finding records based on different fields/columns?

开发者 https://www.devze.com 2023-03-06 05:52 出处:网络
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

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

精彩评论

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