Search code examples
ruby-on-railsrubyruby-on-rails-4reserved-words

Upgrade rails 4.1 to 4.2 column name issue


I'm running a few years old pretty big Rails 4.1 App, where one of the central models has a column called model_name.

After the failed attempt to upgrade the app to rails 4.2, we have found out, that this column name is actually a rails reserved word. This did not come to our attention before, since the rails method which used the name, was a private method. Now, since rails 4.2 this is public, and rails complains big time over the naming confusion.

I really really really don't wanna rename the column, since it's referenced everywhere in our application, even in a lot of serialized data, historic URL's etc.

Any suggestions on an alternative upgrade procedure other than renaming the column?


Solution

  • At least you have to rename model_name in your database and model. Within URL's serialized data etc. you can leave it as it is. You "only" have to use the new accessor of the model. I would recommend against redefining this method within your model class in the long term. You can't predict which 3rd party gem or Rails internal function relies on the behavior of model_name.

    In the short term you can try rename this colum within the database and use:

    def model_name
      read_attribute(:new_column_name)
    end
    
    def model_name=(new_modelname)
      read_write_attribute(:new_column_name, new_modelname)
    end