Found an issue with jsonb column data
Rails 4.2 and ruby 2.10, we are able to fetch jsonb column named data but not in the latest Rails 6.0.4.1 and ruby-3.0.2, it returning empty hash despite having data in the column
Can any body help me please to understand what causing an issue here
rails c
Loading development environment (Rails 4.2.7.1)
2.2.10 :001 > pp TestTable.find(1234)
<TestTable:0x007faba8255188
id:1234,
name: nil,
script_id:123,
is_active: true,
is_default: false,
created_at: Wed, 25 Aug 2021 03:40:33 UTC +00:00,
updated_at: Wed, 25 Aug 2021 03:40:33 UTC +00:00,
contacts_list: "user1",
se_results: {},
data:
{"OS"=>"",
"AUTHOR"=>"user1",
"VERSION"=> "10.3"
},
debug_owner: "",
transition: {},
status: nil>
$ rvm list
=* ruby-2.2.10 [ x86_64 ]
ruby-2.3.0 [ x86_64 ]
ruby-3.0.0 [ x86_64 ]
ruby-3.0.2 [ x86_64 ]
=> - current
=* - current && default
* - default
$
$ rvm use ruby-3.0.2
Using /Users/user2/.rvm/gems/ruby-3.0.2
$ rails c
Resolving dependencies... Loading development environment (Rails 6.0.4.1)
3.0.2 :001 > pp TestTable.find(329470)
#<TestTable:0x00007fa273e8cc28
id: 1234,
name: nil,
script_id:123,
is_active: true,
is_default: false,
created_at: Wed, 25 Aug 2021 03:40:33 UTC +00:00,
updated_at: Wed, 25 Aug 2021 03:40:33 UTC +00:00,
contacts_list: "user1",
se_results: nil,
data: {},
debug_owner: "",
transition: nil,
status: nil>
=>
the migration file
Table is created initially without the jsonb data column
class CreateTestTable < ActiveRecord::Migration
def change
create_table :test_table do |t|
t.string :name
t.belongs_to :script, foreign_key: true
t.boolean :is_active
t.boolean :is_default
t.string :contacts_list
t.string :se_results
t.string :debug_owner
t.string :status
t.timestamps null: false
end
end
end
Later, we added jsonb column using below migration
class ColumnDataJsonb < ActiveRecord::Migration
def change
add_column :test_table, :data , :jsonb
change_column_default :test_table, :data , '{}'
end
end
if I switch back to rails 4.2 and check, I could see and able to fetch the data
Model: test_table.rb
class TestTable < ActiveRecord::Base
DATA_ATTRIBUTES = [ :author, :version ]
store :data, accessors: DATA_ATTRIBUTES, coder: ActiveSupportJsonProxy
end
active_support_json_proxy.rb
class ActiveSupportJsonProxy
def self.dump(obj)
ActiveSupport::JSON.encode(obj) unless obj.nil?
end
def self.load(obj)
if obj.class == String
ActiveSupport::JSON.decode(obj) if obj.present?
else
obj
end
end
end