title: How to update json column in Rails?
body: <p>I have a <code>json</code> column in my <code>Categories</code> table and I want to update each category record with a translation from a json file. I have built the json file so that it contains a categories array and each category has a name and a translation, like so:</p>
  <pre><code>{
    &quot;categories&quot;: [
      {
          &quot;name&quot;: &quot;starter&quot;,
          &quot;message&quot;: &quot;Abs/Анти блокираща система (система против боксуване)&quot;
      },
      {
          &quot;name&quot;: &quot;alternator&quot;,
          &quot;message&quot;: &quot;Алтернатор&quot;
      }
      ...
    ]
  }
  </code></pre>
  <p>I want every category record to be updated with the language key as well as the translation from the file, like so:</p>
  <p><code>{ bg: 'translation from file' }</code></p>
  <p>I have this code</p>
  <pre><code>file = File.read('app/services/translations/files/bg.json')
  data = JSON.parse(file)
  language = File.basename(file, '.json')
  Translations::CategoriesMigrator.call(file: data, language: language)
  
  module Translations
    class CategoriesMigrator &lt; Service
      def initialize(category_repo: Category)
        @category_repo = category_repo
      end
  
      def call(file:, language:)
        file['categories'].each do |category|
          found_category = @category_repo.find_by(name: category['name'])
          found_category.translated_categories[language] = category['message']
          found_category.save
        end
      end
    end
  end
  </code></pre>
  <p>Right now I end up having all categories in a single category record. What am I doing wrong?</p>
  <h3>Update</h3>
  <p>My db migration looks like this:</p>
  <pre><code>class AddTranslatedCategoriesToCategories &lt; ActiveRecord::Migration[5.1]
    def change
      add_column :categories, :translated_categories, :jsonb, null: false, default: {}
      add_index :categories, :translated_categories, using: :gin
    end
  end
  </code></pre>