A problem from corrupt name of Rails migration

There’s a requirement on my current project which is to keep versioning on every model. Any modification made to model should be able to revert back on simply request. As soon as I was briefed for this requirement, I simply said “Acts As Versioned would do this.”

I spent a little time working with it since I ever used it before. Besides, the usage of this plugin is very simple and straightforward, hail to Rick Olson. Consult the post by Juixe here if you want some examples.

However the problem happened when I run the migration file. Rails alerted me that there’s no method named create_versioned_table for Quotation (my model here). I made sure that I already put acts_as_versioned on the Quotation model (which make Quotation now has the abilities of Acts As Versioned) but the problem still exist.

I spent a big while tried to find what caused this problem. I talked with a junior colleague who said he found a similar problem like this before, and fixed it by renaming the migration file. He didn’t know what caused the problem and why it’s fixed that way. So I didn’t buy it at the first because at that moment, I still have no idea what is the relation between the migration’s filename and this problem. Until I noticed that a migration file was named as 005_quotation.rb with class named Quotation

OK, I got it now!

The problem was that when I called Quotation.create_versioned_table in the migration file. The Quotation here is not Quotation model as I intended but it’s migration class named Quotation. Thus it doesn’t know create_versioned_table method. So fix it by renaming the migration class (and filename) to what is should be, CreateQuotation (005_create_quotation.rb).

Problem solved , I’m happy now.


About this entry