Why do you need Arel?

In my opinion Arel is one of the greatest and most underestimated gems. Of course today Arel is also one of the most used gems because it’s shipped in Rails, but how many people use it actively? And why would they?

Since Rails 3 Arel is the base block of ActiveRecord. Every time you pass a hash to where, it goes through Arel eventually. Rails exposes this with a public API that we can use when we have to write complex queries.
Continue reading “Why do you need Arel?”

Let PostgreSQL do its work

Placing things in the right place is important. Rails devs tend to forget this, because Rails gives them some really good helpers.

For example, take a uniqueness validation. Doing the validation on the Rails side can produce duplicated records when there are two concurrent requests. So the best way is to handle this type of validation on the DB side.

Same thing with the counter cache column. Defining it on the Rails side may seem the right solution, but this choice will affect performances. Better to handle it via PostgreSQL.

I found a series of posts about these portings by Sean Huber: