I'm changing the inheritance_column value of a base model, which is extended using STI, in an existing app. How can I write a migration in order to make the existing columns conform with the new inheritance_column?
Here's my first attempt:
class MigrateStoryTypes < ActiveRecord::Migration
def self.up
Story.all.each { |story|
new_story_type = story.story_type.camelize + 'Story'
puts "changing #{story.id}'s story_type from #{story.story_type} to #{new_story_type}"
story.update_column :story_type, new_story_type
}
end
def self.down
Story.all.each { |story|
new_story_type = story.story_type.underscore.gsub /_story/, ''
puts "changing #{story.id}'s story_type from #{story.story_type} to #{new_story_type}"
story.update_column :story_type, new_story_type
}
end
end
However, this fails with:
ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'clean_slate'. This error is raised because the column ' story_type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Story.inheritance_column to use another column for that information.
Is there a straight-forward way of doing this through ActiveRecord or do I need to use a temporary column, SQL, etc.?
story_typecolumn that contains underscored versions of the class name (i.e.'clean_slate') and now you want to move this to STI, usestory_typeas the STI column, and convert thestory_typevalues to class names? How manystory_typevalues do you currently have?story_typevalues.