All threads / how to move existing body content to use action text?

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?

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.

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

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.

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

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

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

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

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

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.