Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Fetching Single objects in Rails Applications

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 567
    Comment on it

    Fetching Single objects in Rails Applications through Active records

     

    While creating a web application in rails we often feel the need to fetch single objects from the database. 

    Active records provide us with a very good query interface where we can fetch single objects from the database without having to write raw queries.

     

    In this blog, we will be seeing three methods through which we can fetch single objects. They are ass follows

    • first

    The first method of Active records helps us to fetch the first record present through the primary key in the table. 

    For example, if we have a user table so it will fetch the first user in the table then be the ID 1 or 2 does not matter.

    user = User.first

    this will fetch the first user in the output like this

    SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1
    
    [#<User id: 23, email: "abc@gmail.com", encrypted_password: "$2a$11$QG14pxXhJOaadkRorgLnLuj0uIv4jUZFw7PzsKM/0U....", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 2, current_sign_in_at: "2016-08-24 13:32:28", last_sign_in_at: "2016-08-05 12:48:27", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2016-08-05 10:37:44", updated_at: "2016-08-24 13:32:28">]

     

    and same can be done for fetching first n number of records like this

    users = User.first(2)

     

    this will give us first 2 users in output from the user table like  

    SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 2
    
    [#<User id: 23, email: "abc@gmail.com", encrypted_password: "$2a$11$QG14pxXhJOaadkRorgLnLuj0uIv4jUZFw7PzsKM/0U....", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 2, current_sign_in_at: "2016-08-24 13:32:28", last_sign_in_at: "2016-08-05 12:48:27", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2016-08-05 10:37:44", updated_at: "2016-08-24 13:32:28", auth_token: "JKJb5CwFryfac3Hb5z-f">, #<User id: 25, email: "def@gmail.com", encrypted_password: "$2a$11$6D/8pOyXlHX8C5OLrgoiY.t9jsXosQsCakIwBbckuQl...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2016-08-22 10:17:14", last_sign_in_at: "2016-08-22 10:17:14", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2016-08-22 10:15:53", updated_at: "2016-08-22 10:46:11">] 
    • take

    The take method fetches a record without keeping the implicit ordering as a role . The example is as follows

    article = Article.take(4)

    this will give us four articles from the articles table without any implicit ordering like this

    SELECT  "articles".* FROM "articles" LIMIT 4
    
    [#<Article id: 4, title: "This is my first article", description: "This is my first article's description.", created_at: "2016-08-05 12:27:53", updated_at: "2016-08-22 10:18:53", user_id: 25, zip_code_id: 7>, #<Article id: 5, title: "This is my second article", description: "This is my second article's description.", created_at: "2016-08-05 12:29:04", updated_at: "2016-08-22 10:19:25", user_id: 25, zip_code_id: 7>, #<Article id: 11, title: "This is my first article from user ID 23", description: "This is my first article's description from user I...", created_at: "2016-08-10 09:27:40", updated_at: "2016-08-10 09:27:40", user_id: 23, zip_code_id: 6>, #<Article id: 12, title: "This is my second article from user ID 23", description: "This is my second article's description from user ...", created_at: "2016-08-10 09:28:32", updated_at: "2016-08-10 09:28:32", user_id: 23, zip_code_id: 6>]

     

    • last

    The last method of Active records helps us to fetch the last record present through the primary key in the table. 

    For example if we have a comment table so it will fetch the last comment in the table like this

    comment = Comment.last

     

    this will fetch the last comment in the output like this

    SELECT  "comments".* FROM "comments"  ORDER BY "comments"."id" DESC LIMIT 1
    
     => #<Comment id: 19, user_id: 23, article_id: 4, comment: "this is nested comment of parent ID 18", created_at: "2016-08-08 13:39:01", updated_at: "2016-08-08 13:39:01", parent_comment_id: 18> 

     

    and the same can be done for getting last n number of comments like this

    comment = Comment.last(3)

     

    and the query which will be fired at the back end would be.

    SELECT  "comments".* FROM "comments"  ORDER BY "comments"."id" DESC LIMIT 3
    
     => [#<Comment id: 17, user_id: 25, article_id: 4, comment: "this is my second comment by ID 22 to article ID 4", created_at: "2016-08-05 12:32:45", updated_at: "2016-08-22 10:32:02", parent_comment_id: nil>, #<Comment id: 18, user_id: 23, article_id: 4, comment: "this is nested comment of comment ID 14", created_at: "2016-08-08 12:35:43", updated_at: "2016-08-08 12:35:43", parent_comment_id: 14>, #<Comment id: 19, user_id: 23, article_id: 4, comment: "this is nested comment of parent ID 18", created_at: "2016-08-08 13:39:01", updated_at: "2016-08-08 13:39:01", parent_comment_id: 18>] 
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: