Ask A Question

Notifications

You’re not receiving notifications from this thread.

how to move existing body content to use action text?

Alan Reid asked in Rails

I followed the great video Chris did on action text, however, I have an existing data structure and want to move it over to use actiontext for editing the body content.

How can i display the old content in the trix editor, if its not been moved over? Is there an easy way?

Reply

That's a good question. I believe you'd have to convert your old content to HTML, then save it in the associated has_rich_text object. ActionText has its own model, so you would just create that record and that should make it available.

I haven't done this, so I don't have any code to point you in the right direction unfortunately.

Reply

mmm, wonder if i can create a worker to do it for me basically save from :product_body to :body. I have like 32,000 records to update.

Great video though, i had it up an running in seconds! Then I sat there and went... "Oh, bugger!" hahaha

Reply

Yup, I had the same thing. I want to convert the GoRails forums over to it but now I'm like shoot...this is gonna be a lot of work to get right.

Reply

Quick update, made a quick worker to take my existing content, and save it to the actiontext table :D Seems to be working a treat!

Now to sit back and wait :D

Reply

Awesome, let us know how it goes! And if it works, do you mind sharing the code?

Reply

Of course, it seems to be working well. Saves a load of time. I sure it could be refactored to be quicker too. This was screated from a worker I am using to push updates to shopify - thus the begin, rescue, ensure

class FixProductBodyWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform()
    batch_index = 1

    Product.where(x: "y")
        .find_in_batches(:batch_size => 100) do | products |

       retries = 3
       begin
         products.each do |product|
          if !product.product_body.nil?

            body_init = product.product_body

            # I added a load of editing stuff here to remove 
                        # unwanted text, or editing parts of the content.

                        # The clear out any whitespace - I do this as a lot 
                        #of the data was scraped from third-party supplier sites.
            body_edit = product.product_body.strip

            product.update(description: body_edit)
          end
         end
       rescue StandardError=>e
         puts "\tError: #{e}"
         if retries > 0
           puts "\tTrying #{retries} more times"
           retries -= 1
           sleep 20
           retry
         else
           puts "\t\tIssues processing batch #{batch_index}, so moving on"
           batch_index += 1
         end
       else
         puts "\tProceesed batch #{batch_index}."
         batch_index += 1
       ensure
         sleep 10
       end
    end
  end
end

Reply

Thanks Alan! I'll have to try something like this on my local copy of GoRails and see how it goes.

Reply

Let me know how you get on. Hope it goes well.

Reply

Hey Alan, did your approach work? This worked for me: https://github.com/rails/rails/issues/35002

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.