Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON type not properly implemented #795

Open
valinaga opened this issue Mar 15, 2022 · 4 comments
Open

JSON type not properly implemented #795

valinaga opened this issue Mar 15, 2022 · 4 comments
Labels

Comments

@valinaga
Copy link

valinaga commented Mar 15, 2022

JSON type not supported for translation

When trying to translate a field of type :json, the output is a string with its stringified value instead.
instead of:

post.options
=> ["0-2 times", "3-4 times", "More than 4 times"]

I got:

post.options
=> "[\"0-2 times\",\"3-4 times\",\"More than 4 times\"]"

Migrations

# Post migration
create_table :posts do |t|
      t.string :title
      t.text :content
      t.json :options
end

Post.create_translation_table!({
          :title => :string,
          :content => :text,
          :options => :json
        }, {
          :migrate_data => true
        })

Any suggestions ?
Thanks

# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.2.0'
gem 'globalize', '5.0.1'
require 'active_record'
require 'globalize'
require 'minitest/autorun'
require 'logger'

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
  end

  create_table :post_translations, force: true do |t|
    t.references :post
    t.string     :title
    t.text       :content
    t.string     :locale
  end
end

class Post < ActiveRecord::Base
  translates :content, :title
end

class BugTest < Minitest::Test
  def test_association_stuff
    post = Post.create!(title: 'HI')

    assert_equal 'HI', post.title
  end
end
@valinaga
Copy link
Author

valinaga commented Mar 16, 2022

looks like the issue happens when the data is migrated. The json data is stringified in the translations table.
Maybe globalize.fetch from read_translated_attribute might be the reason.
On workaround is to update all migrated translations fields with their JSON.parse values.
But maybe you can find a more elegant solution.
Thanks

@stale
Copy link

stale bot commented Apr 27, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 27, 2022
@parndt parndt added the pinned label May 24, 2022
@stale stale bot removed the stale label May 24, 2022
@parndt
Copy link
Member

parndt commented May 24, 2022

Thanks @valinaga 😊

What did you end up doing to solve this for your app?

@valinaga
Copy link
Author

valinaga commented May 24, 2022

no elegant solution found. I had to parse json and individually translate values for each key. thankfully the jsons were simple value-pairs or arrays of value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants