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:
This method is used to get records from the table in a specific order like this
User.order(:created_at)
or we can also write
User.order("created_at")
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.
The limit allows us to set the limit to the records which have to be fetched from the table like this
User.limit(10)
This will get the first 10 records from the user table as right now no offset has been put to the limit method.
The offset method allows us to specify that from which particular order number do we need the records.
User.limit(10).offset(25)
This will fetch 10 users from the user table whose order will be starting from 26th record.
0 Comment(s)