Skip to content

Instantly share code, notes, and snippets.

@stephancom
Created April 27, 2019 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephancom/886e65c46ac0cc045b141adc9d807462 to your computer and use it in GitHub Desktop.
Save stephancom/886e65c46ac0cc045b141adc9d807462 to your computer and use it in GitHub Desktop.
migrate to actiontext (untested)
class MigrateFieldsToActionText < ActiveRecord::Migration[5.2]
class ActionTextRichText < ActiveRecord::Base
belongs_to :record, polymorphic: true
end
TARGETS = {
ModelName => [:field],
OtherModelName => %i[field1 field2 field3]
}.freeze
def up
TARGETS.each do |model, fields|
fields.each do |field|
model.where.not(field => nil).each do |record|
ActionTextRichText.where(
record: record,
name: first
).first_or_create(
body: model.send(field)
)
end
remove_column model.table_name.to_sym, field.to_sym
end
end
end
def down
TARGETS.each do |model, fields|
fields.each do |field|
add_column model.table_name.to_sym, :tmp, :text
ActionTextRichText.where(record_type: model.name, name: field).each do |entry|
entry.record.update_column(field => entry.body)
entry.destroy
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment