Rails is powerful, and one of the reasons of its power is its ability to talk with databases, without having a developer to write the raw SQL queries, and the source of this power is ActiveRecord, a dependency of Rails.

It is ActiveRecord that enables flawless communication between Rails and different relational databases. It is basically a wrapper around your database that provides you with a lot of functionalities. ActiveRecord has evolved over the years, and possesses the sort of functionality one can imagine.

In this article, you will know about the most powerful but lesser-known capabilities of ActiveRecord, and how you can use them in your next project.

Note: You do not need to write anything in your Gemfile to install ActiveRecord, for – it’s the dependency of Rails, so it will be automatically installed for you.

1. Readonly objects:

Your data is all you have got, and for some companies, the data is all their source of income, so why to risk it? On some occasions, you would like to have data that an end user can only read, and be unable to modify it through anyway, and the good news is that you don’t have to take extra steps to make it non-writeable. ActiveRecord provides a method named readonly and this way you will get real objects based on a model and you won’t be able to alter those.

user = User.readyonly.find(parms[:id])

This way, it will give you a user instance that will be only readable, and no way, it could be modified.

2. Using time ranges in querying:

It’s very easy to manipulate time in Ruby, and it has got a bunch of methods to do so. The methods like Time.now , 2.days.ago are very popular in Ruby community, and what makes them more popular is the ability of ActiveRecord to receive those parameters. You can find a set of objects that were created or modified within a specific range of time. For example,

users = User.where(created_at: 3.weeks.ago..2.days.ago

This query mentioned above is going to return you all the users that have been created since 3 weeks ago, excluded the last two days from now. Not only this, you can even subtract 2 Time objects as well to come up with a more fancy way of building a query.

3. pluck and not methods:

In recent version of Rails, two new methods were added among the other methods that can you make your code readable as well as can save you a couple of characters along the way to make your code more concise.

One of them is pluck. Before Rails 3.2, if you were to get specific columns from a table, you would do something like following:

user = User.select(:id, :name).map { |user| [user.id, user.name] }

It will return you an array of all the users, but those users will have only two things: id and name.

Rails 3.2 has simplified this thing, and now you can simply use:

User.pluck(:id, :name)

Not only this, if you were to get the ids of all the users, Rails 4 has got a more readable method for it called ids.

user_ids = User.ids
# same as User.pluck(:id) or User.select(:id).map &:id

The other method available from Rails 4: not that makes it possible to write beautiful queries that use negation.

Instead of writing:

User.where('name IS NOT ?', 'john')

You can simply write:

User.where.not('name IS ?', 'john')

Simply, it reads well.

This is just a glimpse of the fancy world of ActiveRecord in Rails universe. Interested in more, please head over to Official Rails Guide. Happy querying!

Leave a Reply

Your email address will not be published. Required fields are marked *