Skip to content

Rails 4.2.2 vs Rails 5.0.2 JSON Storage with PG #28292

Closed
@kengreeff

Description

@kengreeff

After updating our app from Rails 4.2.2 to Rails 5 we have noticed that JSON strings are being stored as a string rather than a Hash or Array.

Is there a way we can reproduce the behaviour in Rails 5?

Steps to reproduce

  1. Create Model
    bundle exec rails g model OrderItem options:json

  2. Run migration
    bundle exec rake db:migrate

  3. Open Console
    bundle exec rails c

  4. In Console (Rails 4.2.2)

item = OrderItem.new(options: "{\"key\":\"value\"}")
 => #<OrderItem id: nil, options: {"key"=>"value"}>

item.options
 => {"key"=>"value"} 

item.options.is_a?(String)
 => false 

item.options.is_a?(Hash)
 => true
  1. In Console (Rails 5.0.2)
item = OrderItem.new(options: "{\"key\":\"value\"}")
 => #<OrderItem id: nil, options: "{\"key\":\"value\"}">

item.options
 => "{\"key\":\"value\"}"

item.options.is_a?(String)
 => true 

item.options.is_a?(Hash)
 => false

Expected behavior

The options attribute should return a hash

Actual behavior

The options attribute returns a string

System configuration

Rails version: 5.0.2
Ruby version: ruby 2.3.1p112
PostgreSQL: 9.4.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions