0

I've got a model which one of the column is a hash value, like below

[3] pry(main)> Activity.find(14)
=> #<Activity:0x00007fe625694070
 id: 14,
 content_basic: {
"content_type"=>"Audio",
 "session_overview_image"=>"https://some_domain.com/392/123-woman.png"}

I want to change content_basic['session_overview_image'] to be /392/123-woman.png - how to do that in a rails c?

mr_muscle
  • 2,536
  • 18
  • 61

1 Answers1

-1

How about doing it like this:

Activity.find_each do |activity|
  img = activity.content_basic['session_overview_image'].gsub('https://some_domain.com', '')
  activity.content_basic['session_overview_image'] = img
  activity.save
end

This assumes that all of the urls have the same domain that you want to remove. If they don't, then a little bit of URI parsing would be required first.

It also assumes that you are not on one of the older Rails versions (3 of before I believe) where you had to tell ActiveRecord that something has changed in the hash when changing serialized column values.

DanneManne
  • 21,107
  • 5
  • 57
  • 58