Skip to main content
Ask A Question
Notifications
You’re not receiving notifications from this thread.
Subscribe

how to move existing body content to use action text?

Rails • Asked by Alan Reid

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.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 31,152+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.