Ruby On Rails Classroom image

Neha  Jaggi / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  ||
Lessons:- Finding User Objects

Finding User Objects


Active Record provides several options for finding objects. Let’s use them to find the first user we created while verifying that the third user (foo) has been destroyed. We’ll
start with the existing user:

 

>> User.find(1)
=> #<User id: 1, name: "Michael Hartl", email: "mhartl@example.com",
created at: "2011-12-05 00:57:46", updated at: "2011-12-05 00:57:46">

 

Here we’ve passed the id of the user to User.find; Active Record returns the user with that id. 

               Let’s see if the user with an id of 3 still exists in the database:

 

>> User . find(3)
ActiveRecord::RecordNotFound: Couldn't find User with ID=3

 

  Since we destroyed our third user in Section 6.1.3, Active Record can’t find it in the database. Instead, find raises an exception, which is a way of indicating an exceptional event in the execution of a program—in this case, a nonexistent Active Record id, which causes find to raise an ActiveRecord::RecordNotFound exception.8 In addition to the

                generic find, Active Record also allows us to find users by specific attributes:

 

>> User .find  by  email   ("mhartl@example.com")
=> #<User id: 1, name: "Michael Hartl", email: "mhartl@example.com",
created at: "2011-12-05 00:57:46", updated at: "2011-12-05 00:57:46">

 

 

The find_by_email method is automatically created by Active Record based on the email attribute in the users table. (As you might guess, Active Record creates a find_by_name method as well.) Since we will be using email addresses as usernames, this sort of find will be useful when we learn how to let users sign in to our site (Chapter 7). If you’re worried that find_by_email will be inefficient if there are a large number of users, you’re ahead of the game; we’ll cover this issue, and its solution via database indices, in Section 6.2.5. 

            We’ll end with a couple of more general ways of finding users. First, there’s first:

 

>> User.first
=> #<User id: 1, name: "Michael Hartl", email: "mhartl@example.com",
created at: "2011-12-05 00:57:46", updated at: "2011-12-05 00:57:46">

 

Naturally, first just returns the first user in the database. There’s also all: 

 

>> User .all
=> [#<User id: 1, name: "Michael Hartl", email: "mhartl@example.com",
created at: "2011-12-05 00:57:46", updated at: "2011-12-05 00:57:46">,
#<User id: 2, name: "A Nother", email: "another@example.org", created at:
"2011-12-05 01:05:24", updated at: "2011-12-05 01:05:24">]

 

No prizes for inferring that all returns an array (Section 4.3.1) of all users in the database. 

 
 
 
image
Neha  Jaggi

Skills    Ruby On Rails

Qualifications :- High School - , College/University - Graphic Era Hill University, Dehradun, College/University - ,
Location :-Dehradun,Dehradun,UTTARAKHAND,India
Description:-

Experienced Software Developer with a demonstrated history of working in the Information Technology and services industry. Skilled in Web Technologies (Ruby on Rails, PostgreSQL, php, Laravel and AJAX). 


Explore
 

  Students (0)