Archive for ActiveRecord

ActiveRecord Associations Extensions

Here is a little known feature of ActiveRecord’s Associations that rarely gets mentioned. Even though it has been documented, it rarely makes any news. I love this feature.

Basic Associations

So lets assume I have two models: Student and Assignment, where a Student has many Assignments, and thus the Assignment belongs to the Student.

Lets also assume that the Assignment has a field called ‘state‘, indicating whether it is submitted, finished, hasn’t started, procrastinating, or eaten by the dog.

The general way to find assignments of a particular state is to use the find method with a set of conditions.

The first line, finds all submitted assignments, and the second one, all incomplete assignments.

But it’s tedious to type this up each and every time we want to find this sort of assignments. Also you can make mistakes, and it makes your code very messy.

Extending Associations

So let’s move these finder methods into the association itself. I present to you our new Student class!

And now we can find all submitted assignments and all incomplete assignments like such:

Doesn’t that just feel good?

But if you are like me, you’ll keep adding more and more methods to the extension. Which will start making it once again, messy and difficult to read. Damn

Extending Associations into a Module

So once again we can extract it out, and this time store it into a module.

You can keep this file wherever you like, but just to keep things simple, I suggest leaving the extension in the model’s file.

Note: The extension/module is not specific to the model. If other model’s have the same needs, you can reuse this module.

Comments (3)

Avoid Using A Field Called ’status’

status‘ sounds like a good name for a field name doesn’t it? It can indicate whether your Post is draft, published or private. Or may be it can be used to indicate the current stage that your Product is in, ie: manufacturing, cleaning, testing, complete.

Surely sound’s like a brilliant solution doesn’t it? And it is actually!

Except for one thing.

Avoid Using a ’select’ Field

Except MySQL uses the keyword ‘status‘. So if you wanted to do queries like so:

you would need to enclose the ‘status‘ field in back ticks, like so:

Now that isn’t cool at all, because using find method with conditions can be highly irritating, and doesn’t look clean at all.

So if you actually do decide to use a field called ’status’ and enclose it within back ticks, then you’ll run into another problem. Database independence.

SQLite does not support back ticks. So if you like to develop and test on SQLite initially, and similarly since your application has been comfortable with SQLite, you decide to also use it production. But what happens when SQLite can’t scale anymore? Well you’ve got to move elsewhere. Unh unh right?

Note: I am inexperienced with PostgreSQL, so maybe others can update me on this.

Alternatives

Other alternative I have been using is to call the field ‘stage‘ or ‘state‘. There are probably tons of other possibilities out there, but do avoid using a field called ’status’.

Any suggestions for other possible alternatives to use?

Comments (2)

Next entries »