I have a user model that checks to see if a value has changed before_save (by running User.zipcode_changed?). The idea is that this will queue up a delayed job if it has.
Problem is, when I migrate the app from scratch I get an error:
An error has occurred, all later migrations canceled:
undefined method `postcode_changed?' for #<User:0x105db6158>
Therefore, where should I be putting these? Is the model the wrong place?
When you checkout a new project from scratch, you shouldn’t use migrations to build the database.
You should use
rake db:schema:loadinstead.Let me show you why.
Let’s assume you create a new
Postmodel with a post table on migration 10.On migration 11, you execute some special elaborations on the
Postmodel.After a while, you decide to drop the
Postmodel and the post table because no longer required.Six month later, you checkout the project from scratch. If you try to run
rake db:migratethe migration 11 will fail complaining about missing model. It’s true, the model has been removed many month before and it’s no longer available.Instead, if you run
rake db:schema:loadyou’ll initialize the database with the right schema version.Talking about migrations, if you just created the postcode method and you are trying to use the _changed? magic method in the same migration, you need to reload the schema before.