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: