{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/26991","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/26991/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/26991/comments","events_url":"https://api.github.com/repos/rails/rails/issues/26991/events","html_url":"https://github.com/rails/rails/issues/26991","id":187827287,"number":26991,"title":"Unchanged activerecord object has changed? = true with rails5 + postgres json[] column + special characters","user":{"login":"nbrustein","id":82556,"avatar_url":"https://avatars.githubusercontent.com/u/82556?v=3","gravatar_id":"","url":"https://api.github.com/users/nbrustein","html_url":"https://github.com/nbrustein","followers_url":"https://api.github.com/users/nbrustein/followers","following_url":"https://api.github.com/users/nbrustein/following{/other_user}","gists_url":"https://api.github.com/users/nbrustein/gists{/gist_id}","starred_url":"https://api.github.com/users/nbrustein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nbrustein/subscriptions","organizations_url":"https://api.github.com/users/nbrustein/orgs","repos_url":"https://api.github.com/users/nbrustein/repos","events_url":"https://api.github.com/users/nbrustein/events{/privacy}","received_events_url":"https://api.github.com/users/nbrustein/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-11-07T21:01:54Z","updated_at":"2016-11-07T21:06:53Z","closed_at":null,"body":"If an active record object has a json[] property that includes a special character, then `changed?` will be true even when nothing has changed.\r\n\r\n### Steps to reproduce\r\n\r\n * Create an active record class that has a column of type `json[]`\r\n * Store a value in that array that includes a special character like `Ѭ`\r\n * Load the row in active record\r\n * call `attributes` on the active record instance\r\n * `changed?` will be true, even before you make any changes\r\n\r\nHere is a test showing this behavior\r\n\r\n````\r\nit \"should not be changed once it is loaded up\" do\r\n    instance = MyModel.new\r\n    instance.assign_attributes({\r\n        \"my_json_array\" => [{\"description\"=>\"Ѭ\"}]\r\n    })\r\n    instance.save!\r\n    reloaded = MyModel.find(instance.id)\r\n\r\n    expect(reloaded.changed?).to be(false) # this will pass\r\n    reloaded.attributes\r\n    expect(reloaded.changed?).to be(false) # this will fail\r\nend\r\n````\r\n\r\nHere is a test showing that this behavior is coming from `ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array`\r\n\r\n````\r\nit \"should not change in serialization in rails 5\" do\r\n    instance = ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.new(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Json.new)\r\n    str = \"{\\\"{\\\\\\\"description\\\\\\\":\\\\\\\"Ѭ\\\\\\\"}\\\"}\"\r\n    deserialized = instance.deserialize(str)\r\n    expect(instance.serialize(deserialized)).to eq(str) # this fails\r\nend\r\n````\r\n\r\nAnd here is a test showing what I think is the analogous code from rails 4, which passes\r\n````\r\nit \"should not have a broken array class in rails 4\" do\r\n    instance = ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.new(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Json.new)\r\n    str = \"{\\\"{\\\\\\\"description\\\\\\\":\\\\\\\"Ѭ\\\\\\\"}\\\"}\"\r\n    deserialized = instance.type_cast_from_database(str)\r\n    expect(instance.type_cast_for_database(deserialized)).to eq(str) # this passes\r\nend\r\n````\r\n\r\n### Expected behavior\r\nWhen I load an instance from the database with a special character inside of a column of the `json[]` type, `changed?` should be false when I haven't made any changes\r\n\r\n### Actual behavior\r\nWhen I load an instance from the database with a special character inside of a column of the `json[]` type, `changed?` is true once I have called `attributes`\r\n\r\n### System configuration\r\n**Rails version**:\r\nRails 5.0.0.1\r\n\r\n**Ruby version**:\r\nruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/258962662","html_url":"https://github.com/rails/rails/issues/26991#issuecomment-258962662","issue_url":"https://api.github.com/repos/rails/rails/issues/26991","id":258962662,"user":{"login":"nbrustein","id":82556,"avatar_url":"https://avatars.githubusercontent.com/u/82556?v=3","gravatar_id":"","url":"https://api.github.com/users/nbrustein","html_url":"https://github.com/nbrustein","followers_url":"https://api.github.com/users/nbrustein/followers","following_url":"https://api.github.com/users/nbrustein/following{/other_user}","gists_url":"https://api.github.com/users/nbrustein/gists{/gist_id}","starred_url":"https://api.github.com/users/nbrustein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nbrustein/subscriptions","organizations_url":"https://api.github.com/users/nbrustein/orgs","repos_url":"https://api.github.com/users/nbrustein/repos","events_url":"https://api.github.com/users/nbrustein/events{/privacy}","received_events_url":"https://api.github.com/users/nbrustein/received_events","type":"User","site_admin":false},"created_at":"2016-11-07T21:06:53Z","updated_at":"2016-11-07T21:06:53Z","body":"@sgrif I saw that you have a lot of activity in `connection_adapters/postgresql/oid/array.rb`, so I thought this issue might be interesting to you, and you might have some suggestions.  I hope it's not rude to point it out to you.  I don't mean to suggest that I expect you to fix it ;)"}}
{"actor":{"display_login":"nbrustein"}}