{"action":"created","issue":{"url":"https://api.github.com/repos/zdennis/activerecord-import/issues/424","repository_url":"https://api.github.com/repos/zdennis/activerecord-import","labels_url":"https://api.github.com/repos/zdennis/activerecord-import/issues/424/labels{/name}","comments_url":"https://api.github.com/repos/zdennis/activerecord-import/issues/424/comments","events_url":"https://api.github.com/repos/zdennis/activerecord-import/issues/424/events","html_url":"https://github.com/zdennis/activerecord-import/issues/424","id":232071148,"number":424,"title":"Importing saved records with serialized fields","user":{"login":"Andreis13","id":4076570,"avatar_url":"https://avatars3.githubusercontent.com/u/4076570?v=3","gravatar_id":"","url":"https://api.github.com/users/Andreis13","html_url":"https://github.com/Andreis13","followers_url":"https://api.github.com/users/Andreis13/followers","following_url":"https://api.github.com/users/Andreis13/following{/other_user}","gists_url":"https://api.github.com/users/Andreis13/gists{/gist_id}","starred_url":"https://api.github.com/users/Andreis13/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Andreis13/subscriptions","organizations_url":"https://api.github.com/users/Andreis13/orgs","repos_url":"https://api.github.com/users/Andreis13/repos","events_url":"https://api.github.com/users/Andreis13/events{/privacy}","received_events_url":"https://api.github.com/users/Andreis13/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-05-29T17:37:28Z","updated_at":"2017-06-02T00:47:19Z","closed_at":null,"body":"When importing ActiveRecord objects, there is a difference in behaviour when importing newly initialized objects versus objects that have been read from the database.\r\n\r\nFor instance, when a record initialized from arguments is imported, serialized attributes behave as expected: \r\n```ruby\r\nnew_record = Widget.new(json_data: { a: :b })\r\nWidget.import([new_record])\r\n\r\nWidget.last.json_data #=> { a: :b }\r\n```\r\n\r\nMeanwhile, if we read the object from the database and import it (this use-case came up when trying to copy data from one database to another), the JSON attribute is not persisted properly:\r\n```ruby\r\nexisting_record = Widget.last\r\nWidget.import([existing_record])\r\n\r\nWidget.last.json_data #=> \"{\\\"a\\\":\\\"b\\\"}\"\r\n```\r\n\r\nThe reason for such difference in behaviour is the way `read_attribute_before_type_cast` works for new, and persisted records respectively.\r\n\r\n```ruby\r\nWidget.new(json_data: { a: :b }).read_attribute_before_type_cast(:json_data) #=> { a: :b }\r\nWidget.last.read_attribute_before_type_cast(:json_data) #=> \"{\\\"a\\\":\\\"b\\\"}\"\r\n```\r\n\r\nAs far as I understand, the use of `read_attribute_before_type_cast` [in the code](https://github.com/zdennis/activerecord-import/blob/master/lib/activerecord-import/import.rb#L447), is primarily for performance reasons, so using only `read_attribute` wouldn't be a desirable option. Is it possible to somehow handle such a use-case in the future releases of the gem?"},"comment":{"url":"https://api.github.com/repos/zdennis/activerecord-import/issues/comments/305658897","html_url":"https://github.com/zdennis/activerecord-import/issues/424#issuecomment-305658897","issue_url":"https://api.github.com/repos/zdennis/activerecord-import/issues/424","id":305658897,"user":{"login":"daslicious","id":2091181,"avatar_url":"https://avatars1.githubusercontent.com/u/2091181?v=3","gravatar_id":"","url":"https://api.github.com/users/daslicious","html_url":"https://github.com/daslicious","followers_url":"https://api.github.com/users/daslicious/followers","following_url":"https://api.github.com/users/daslicious/following{/other_user}","gists_url":"https://api.github.com/users/daslicious/gists{/gist_id}","starred_url":"https://api.github.com/users/daslicious/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/daslicious/subscriptions","organizations_url":"https://api.github.com/users/daslicious/orgs","repos_url":"https://api.github.com/users/daslicious/repos","events_url":"https://api.github.com/users/daslicious/events{/privacy}","received_events_url":"https://api.github.com/users/daslicious/received_events","type":"User","site_admin":false},"created_at":"2017-06-02T00:47:19Z","updated_at":"2017-06-02T00:47:19Z","body":"`Widget.new(json_data: { a: :b })` will import with `json_data` being empty for me.\r\n\r\nI'm working around it with \r\n\r\n```\r\nwidget = Widget.new(json_data: { a: :b })\r\nwidget.json_data = widget.json_data\r\nwidget\r\n```"}}
{"actor":{"display_login":"daslicious"}}