{"action":"closed","number":76,"pull_request":{"url":"https://api.github.com/repos/state-machines/state_machines-activerecord/pulls/76","id":224094353,"node_id":"MDExOlB1bGxSZXF1ZXN0MjI0MDk0MzUz","html_url":"https://github.com/state-machines/state_machines-activerecord/pull/76","diff_url":"https://github.com/state-machines/state_machines-activerecord/pull/76.diff","patch_url":"https://github.com/state-machines/state_machines-activerecord/pull/76.patch","issue_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues/76","number":76,"state":"closed","locked":false,"title":"Fix StateMachine initial state with AR#create_with:","user":{"login":"Edouard-chin","id":8122246,"node_id":"MDQ6VXNlcjgxMjIyNDY=","avatar_url":"https://avatars0.githubusercontent.com/u/8122246?v=4","gravatar_id":"","url":"https://api.github.com/users/Edouard-chin","html_url":"https://github.com/Edouard-chin","followers_url":"https://api.github.com/users/Edouard-chin/followers","following_url":"https://api.github.com/users/Edouard-chin/following{/other_user}","gists_url":"https://api.github.com/users/Edouard-chin/gists{/gist_id}","starred_url":"https://api.github.com/users/Edouard-chin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Edouard-chin/subscriptions","organizations_url":"https://api.github.com/users/Edouard-chin/orgs","repos_url":"https://api.github.com/users/Edouard-chin/repos","events_url":"https://api.github.com/users/Edouard-chin/events{/privacy}","received_events_url":"https://api.github.com/users/Edouard-chin/received_events","type":"User","site_admin":false},"body":"- In rails/rails@366e7e3 a change was\r\n  made in order to fix an issue where `Record.create_with` could end\r\n  up creating multiple nested records.\r\n  That change had a side effect on the implementation of the\r\n  activerecord state machine library.\r\n\r\n  The TL;DR is that an attribute backed by state machine can't be\r\n  initialized with the value you want and will always be the value of\r\n  the initial state. You can find a reproduction test case here\r\n  https://gist.github.com/Edouard-chin/6abff4cd230006a2c22cb341c198e34e\r\n\r\n  The main difference before the rails change mentioned above, is that\r\n  all values passed to `create_with` were merged with the attributes.\r\n  I.e, assuming that `state` attribute is backed by a state machine,\r\n  calling this:\r\n\r\n  `User.create_with(state: 'created').find_or_create_by(age: '29')`\r\n\r\n  would result in a hash like `{ state: 'created', age: '29' }`. This\r\n  hash was then passed to the `User` constructor which is overrided\r\n  [here](https://github.com/state-machines/state_machines-activerecord/blob/fed06d9fa64af1cba49d241f4a3ae79626946fe3/lib/state_machines/integrations/active_record.rb#L463)\r\n  State Machine Gem was then checking the hash and would not override\r\n  the `state` attribute if it was already present.\r\n\r\n  Now, with the Rails change, the attributes hash looks like this\r\n  `{ age: '29' }`. The state disappeared, it get set in another way\r\n  thanks to `create_with_value`\r\n  https://github.com/rails/rails/blob/517a54501d2de1225327296adde8efb91774b237/activerecord/lib/active_record/scoping.rb#L36\r\n  Because the statemachine library doesn't receive a hash with the\r\n  `state` key it thinks that the state wasn't set and thus assign a\r\n  default value.\r\n\r\n  The fix on this PR is to pass the attributes as well as the\r\n  scoped_attributes when initializing the state machine.\r\n\r\ncc/ @rafaelfranca ","created_at":"2018-10-18T20:48:44Z","updated_at":"2018-10-19T21:32:16Z","closed_at":"2018-10-19T21:32:16Z","merged_at":"2018-10-19T21:32:16Z","merge_commit_sha":"52f79a94b2cb13acac6dd654fe2b2578d2589200","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/pulls/76/commits","review_comments_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/pulls/76/comments","review_comment_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/pulls/comments{/number}","comments_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues/76/comments","statuses_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/statuses/3ed003703c9b11d6a0c846a7baa198488a76ce56","head":{"label":"Edouard-chin:rails-5-2-create-with","ref":"rails-5-2-create-with","sha":"3ed003703c9b11d6a0c846a7baa198488a76ce56","user":{"login":"Edouard-chin","id":8122246,"node_id":"MDQ6VXNlcjgxMjIyNDY=","avatar_url":"https://avatars0.githubusercontent.com/u/8122246?v=4","gravatar_id":"","url":"https://api.github.com/users/Edouard-chin","html_url":"https://github.com/Edouard-chin","followers_url":"https://api.github.com/users/Edouard-chin/followers","following_url":"https://api.github.com/users/Edouard-chin/following{/other_user}","gists_url":"https://api.github.com/users/Edouard-chin/gists{/gist_id}","starred_url":"https://api.github.com/users/Edouard-chin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Edouard-chin/subscriptions","organizations_url":"https://api.github.com/users/Edouard-chin/orgs","repos_url":"https://api.github.com/users/Edouard-chin/repos","events_url":"https://api.github.com/users/Edouard-chin/events{/privacy}","received_events_url":"https://api.github.com/users/Edouard-chin/received_events","type":"User","site_admin":false},"repo":{"id":153685400,"node_id":"MDEwOlJlcG9zaXRvcnkxNTM2ODU0MDA=","name":"state_machines-activerecord","full_name":"Edouard-chin/state_machines-activerecord","private":false,"owner":{"login":"Edouard-chin","id":8122246,"node_id":"MDQ6VXNlcjgxMjIyNDY=","avatar_url":"https://avatars0.githubusercontent.com/u/8122246?v=4","gravatar_id":"","url":"https://api.github.com/users/Edouard-chin","html_url":"https://github.com/Edouard-chin","followers_url":"https://api.github.com/users/Edouard-chin/followers","following_url":"https://api.github.com/users/Edouard-chin/following{/other_user}","gists_url":"https://api.github.com/users/Edouard-chin/gists{/gist_id}","starred_url":"https://api.github.com/users/Edouard-chin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Edouard-chin/subscriptions","organizations_url":"https://api.github.com/users/Edouard-chin/orgs","repos_url":"https://api.github.com/users/Edouard-chin/repos","events_url":"https://api.github.com/users/Edouard-chin/events{/privacy}","received_events_url":"https://api.github.com/users/Edouard-chin/received_events","type":"User","site_admin":false},"html_url":"https://github.com/Edouard-chin/state_machines-activerecord","description":"StateMachines Active Record Integration","fork":true,"url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord","forks_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/forks","keys_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/teams","hooks_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/hooks","issue_events_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/issues/events{/number}","events_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/events","assignees_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/assignees{/user}","branches_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/branches{/branch}","tags_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/tags","blobs_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/git/refs{/sha}","trees_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/statuses/{sha}","languages_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/languages","stargazers_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/stargazers","contributors_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/contributors","subscribers_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/subscribers","subscription_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/subscription","commits_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/commits{/sha}","git_commits_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/git/commits{/sha}","comments_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/comments{/number}","issue_comment_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/issues/comments{/number}","contents_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/contents/{+path}","compare_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/merges","archive_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/downloads","issues_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/issues{/number}","pulls_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/pulls{/number}","milestones_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/milestones{/number}","notifications_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/labels{/name}","releases_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/releases{/id}","deployments_url":"https://api.github.com/repos/Edouard-chin/state_machines-activerecord/deployments","created_at":"2018-10-18T20:45:49Z","updated_at":"2018-10-18T20:45:51Z","pushed_at":"2018-10-19T04:52:28Z","git_url":"git://github.com/Edouard-chin/state_machines-activerecord.git","ssh_url":"git@github.com:Edouard-chin/state_machines-activerecord.git","clone_url":"https://github.com/Edouard-chin/state_machines-activerecord.git","svn_url":"https://github.com/Edouard-chin/state_machines-activerecord","homepage":"https://github.com/state-machines/state_machines-activerecord","size":86,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"state-machines:master","ref":"master","sha":"fed06d9fa64af1cba49d241f4a3ae79626946fe3","user":{"login":"state-machines","id":7613709,"node_id":"MDEyOk9yZ2FuaXphdGlvbjc2MTM3MDk=","avatar_url":"https://avatars0.githubusercontent.com/u/7613709?v=4","gravatar_id":"","url":"https://api.github.com/users/state-machines","html_url":"https://github.com/state-machines","followers_url":"https://api.github.com/users/state-machines/followers","following_url":"https://api.github.com/users/state-machines/following{/other_user}","gists_url":"https://api.github.com/users/state-machines/gists{/gist_id}","starred_url":"https://api.github.com/users/state-machines/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/state-machines/subscriptions","organizations_url":"https://api.github.com/users/state-machines/orgs","repos_url":"https://api.github.com/users/state-machines/repos","events_url":"https://api.github.com/users/state-machines/events{/privacy}","received_events_url":"https://api.github.com/users/state-machines/received_events","type":"Organization","site_admin":false},"repo":{"id":28869403,"node_id":"MDEwOlJlcG9zaXRvcnkyODg2OTQwMw==","name":"state_machines-activerecord","full_name":"state-machines/state_machines-activerecord","private":false,"owner":{"login":"state-machines","id":7613709,"node_id":"MDEyOk9yZ2FuaXphdGlvbjc2MTM3MDk=","avatar_url":"https://avatars0.githubusercontent.com/u/7613709?v=4","gravatar_id":"","url":"https://api.github.com/users/state-machines","html_url":"https://github.com/state-machines","followers_url":"https://api.github.com/users/state-machines/followers","following_url":"https://api.github.com/users/state-machines/following{/other_user}","gists_url":"https://api.github.com/users/state-machines/gists{/gist_id}","starred_url":"https://api.github.com/users/state-machines/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/state-machines/subscriptions","organizations_url":"https://api.github.com/users/state-machines/orgs","repos_url":"https://api.github.com/users/state-machines/repos","events_url":"https://api.github.com/users/state-machines/events{/privacy}","received_events_url":"https://api.github.com/users/state-machines/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/state-machines/state_machines-activerecord","description":"StateMachines Active Record Integration","fork":false,"url":"https://api.github.com/repos/state-machines/state_machines-activerecord","forks_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/forks","keys_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/keys{/key_id}","collaborators_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/teams","hooks_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/hooks","issue_events_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues/events{/number}","events_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/events","assignees_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/assignees{/user}","branches_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/branches{/branch}","tags_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/tags","blobs_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/git/refs{/sha}","trees_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/git/trees{/sha}","statuses_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/statuses/{sha}","languages_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/languages","stargazers_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/stargazers","contributors_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/contributors","subscribers_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/subscribers","subscription_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/subscription","commits_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/commits{/sha}","git_commits_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/git/commits{/sha}","comments_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/comments{/number}","issue_comment_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues/comments{/number}","contents_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/contents/{+path}","compare_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/compare/{base}...{head}","merges_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/merges","archive_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/downloads","issues_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues{/number}","pulls_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/pulls{/number}","milestones_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/milestones{/number}","notifications_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/labels{/name}","releases_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/releases{/id}","deployments_url":"https://api.github.com/repos/state-machines/state_machines-activerecord/deployments","created_at":"2015-01-06T15:29:46Z","updated_at":"2018-10-17T05:39:26Z","pushed_at":"2018-10-19T21:32:16Z","git_url":"git://github.com/state-machines/state_machines-activerecord.git","ssh_url":"git@github.com:state-machines/state_machines-activerecord.git","clone_url":"https://github.com/state-machines/state_machines-activerecord.git","svn_url":"https://github.com/state-machines/state_machines-activerecord","homepage":"https://github.com/state-machines/state_machines-activerecord","size":85,"stargazers_count":257,"watchers_count":257,"language":"Ruby","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":58,"mirror_url":null,"archived":false,"open_issues_count":13,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":58,"open_issues":13,"watchers":257,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/state-machines/state_machines-activerecord/pulls/76"},"html":{"href":"https://github.com/state-machines/state_machines-activerecord/pull/76"},"issue":{"href":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues/76"},"comments":{"href":"https://api.github.com/repos/state-machines/state_machines-activerecord/issues/76/comments"},"review_comments":{"href":"https://api.github.com/repos/state-machines/state_machines-activerecord/pulls/76/comments"},"review_comment":{"href":"https://api.github.com/repos/state-machines/state_machines-activerecord/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/state-machines/state_machines-activerecord/pulls/76/commits"},"statuses":{"href":"https://api.github.com/repos/state-machines/state_machines-activerecord/statuses/3ed003703c9b11d6a0c846a7baa198488a76ce56"}},"author_association":"NONE","merged":true,"mergeable":null,"rebaseable":null,"mergeable_state":"unknown","merged_by":{"login":"rafaelfranca","id":47848,"node_id":"MDQ6VXNlcjQ3ODQ4","avatar_url":"https://avatars3.githubusercontent.com/u/47848?v=4","gravatar_id":"","url":"https://api.github.com/users/rafaelfranca","html_url":"https://github.com/rafaelfranca","followers_url":"https://api.github.com/users/rafaelfranca/followers","following_url":"https://api.github.com/users/rafaelfranca/following{/other_user}","gists_url":"https://api.github.com/users/rafaelfranca/gists{/gist_id}","starred_url":"https://api.github.com/users/rafaelfranca/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rafaelfranca/subscriptions","organizations_url":"https://api.github.com/users/rafaelfranca/orgs","repos_url":"https://api.github.com/users/rafaelfranca/repos","events_url":"https://api.github.com/users/rafaelfranca/events{/privacy}","received_events_url":"https://api.github.com/users/rafaelfranca/received_events","type":"User","site_admin":false},"comments":0,"review_comments":0,"maintainer_can_modify":false,"commits":2,"additions":15,"deletions":3,"changed_files":2}}
{"actor":{"display_login":"rafaelfranca"}}