5 Must Know Active Record Methods
So you just learned about Active Record, but you’re probably wondering what exactly can I do with this 🧐.
Let’s face it, coders are always looking for ways to minimize the amount of work they have to do. Active Record is just the tool to achieve this. Active Record is a great tool that can really help us, as coders, minimize the amount of code we write(among many other great features). Today, I am going to discuss some great query methods provided through active record that can make your life so much easier, because who doesn’t like easy!?
Ordering
First up, we have the ordering method. This can be used to organize our database in a specified way. We can order, alphabetically, numerically, ascending order, descending order, etc.
How do we use it?
To use the ordering method we can call .order on a class like so:
We should pass in an argument to specify which attribute will be used to create this order. We can also pass in an argument for how we would like the order to be (asc or desc), but if this is not specified, the order will be ascending.
Selecting
Select is used to select specific attribute columns. Its return value is an instance(s). Instances require an id field, therefore, if you are not selecting an id attribute field, the id for the instances will be set at nil.
How do we use it?
We can call .select on a class and specify which columns we would like to select within the parameters.
We can also pass in multiple arguments to select multiple columns, like so:
Note: If you will need to make any associations with this new array of instances, it would probably be best to select the id column as well, or else you may receive an error when trying to make an association.
Distinct
Distinct is used to weed out duplicates. Distinct will only return unique/non repeated values. This will not delete any duplicates from our database. It is just a way of only returning unique values.
How do we use it?
We will call .distinct on a class, typically along side another method.
It is best utilized along with our select method, join method, or any method that may be specific to a specified attribute. Why? Sometimes you may find, keeping with the movies theme, you have two instances of Movie that may have all the same field values, but you need to also be aware that the id will be different for each case, because our database will not generate duplicate ids.
Group
Group can be used to group items based on an attribute column. The return value will be an array of unique instances, based on the specified attribute column.
How do we use it?
We simply call .group on a class and pass in the column name we would like to group by in the parameters.
This method gets pretty useful when you call .count on top of this. This would return a count of each distinct value within the called upon attribute column.
Pluck
Pluck says goodbye to our long code of looping through to select and map specific data. Pluck returns an array of the values for the specified attribute columns.
How do we use it?
We will call .pluck on a class and pass in the column names within the parameters.
That was just 5 of the many useful methods provided to us through Active Record. You can find a full list of methods, as well as other useful Active Record information to reference in the Rails Guide.
Hopefully you found this helpful and finally put some of those Active Record Methods to good use when you build out your next project.
If you’re familiar with Active Record methods, what are some of favorites/most used?
Happy Coding!