We build Web & Mobile Applications.

< All Articles

Rails 2.0: validates_acceptance_of can break migrations

UPDATE! Rails 2.0.2 has now been released and includes the fix for this problem.

If you have a model that uses validates_acceptance_of, you have an observer for that model and you try to run the migration responsible for creating the underlying table then the db:migrate task will fail. The actual error you receive will depend upon your database engine – on MySQL it looks something like this:

Mysql::Error: Table 'database_name.table_name' doesn't exist: SHOW FIELDS FROM `table_name`

The culprit is changeset 8208 which allows the attribute used by validates_acceptance_of to actually exist in the database rather than being virtual. When running the migration to create the underlying table, environment.rb causes the model observer to be loaded which in turn loads the model itself. When the model is loaded validates_acceptance_of tries to determine the column names of the (as yet non-existent) table and, yes you guessed it, blows up!

The good news is that changeset 8377 was submitted 4 days ago to fix this problem. To take advantage of it you’ll either have to freeze Rails to the latest revision or you can simply apply the patch if you’re using something like Piston to manage vendor/rails.

Updated on 15 October 2015
First published by Rob Anderton on 16 December 2007
© TheWebFellas Limited 2016
"Rails 2.0: validates_acceptance_of can break migrations" by Rob Anderton at TheWebFellas is licensed under a Creative Commons Attribution 4.0 International License.