Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Ordering, Limit and Offset in Active Records Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 332
    Comment on it

    Active records provide us with ways to set constraints in our queries to specify the order in which we have to get the records from the table.

    This can be done through the following ways: 

    • order

    This method is used to get records from the table in a specific order like this


    or we can also write


    This method will get us the users in the order they were created.

    We can also refine the ordering in by putting in more constraints like this

    User.order("created_at ASC")

    This will get the users in ascending order of when they were created.

    User.order("created_at DESC")

    and this will get the users in descending order of when they were created.


    • limit

    The limit allows us to set the limit to the records which have to be fetched from the table like this


    This will get the first 10 records from the user table as right now no offset has been put to the limit method.


    • offset

    The offset method allows us to specify that from which particular order number do we need the records. 


    This will fetch 10 users from the user table whose order will be starting from 26th record.

 0 Comment(s)

Sign In

Sign up using

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: