Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.