mayorsanmayor
mayorsanmayor

Reputation: 2988

Replacing Ckeditor with ActionText in a Rails 6 app

So I replaced CKEditor with ActionText in my rails 6 application, the upgrade to rails 6 and installation of action text was smooth.

I want to ask how I can be able to migrate the data from my model attribute to the newly established action text association (well not exactly migrate, I want to be able to display the old data and even be able to edit/update it).

For example, I have a description attribute in my model that was used with CKEditor before, now I have changed that field to a rich_text field like this: has_rich_text :description

So now all references to description simply query its rich_text association.

If I wanted to do something like this in my view, how can I achieve that?

@model.description (display if rich_text data is present) || @model.description (or try this if rich_text data is blank, also display nothing if both is blank) 

I'd like to achieve this for show, edit, update and delete actions. Any idea how to make this work?

Upvotes: 3

Views: 1028

Answers (1)

dnwilson
dnwilson

Reputation: 147

Why not just migrate the Ckeditor columns to the the new action_text table?

class ConvertCkeditorToActionText < ActiveRecord::Migration[6.0]
  def up
    action_text_rich_text_statement = ActiveRecord::Base.connection.raw_connection.prepare('action_text_rich_text_statement', <<-SQL)
      INSERT INTO action_text_rich_texts (
        name, body, record_type, record_id, created_at, updated_at
      ) VALUES ($1, $2, $3, $4, $5, $6)
    SQL

    Rails.application.eager_load!

    transaction do
      Post.all.each do |post|
        ActiveRecord::Base.connection.raw_connection.exec_prepared(
                'action_text_rich_text_statement', [
                  'body',
                  post.body,
                  "Post",
                  post.id,
                  post.created_at,
                  post.updated_at
                ])
      end
    end
  end

  def down
    raise ActiveRecord::IrreversibleMigration
  end
end

Upvotes: 2

Related Questions