{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/18573","labels_url":"https://api.github.com/repos/rails/rails/issues/18573/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/18573/comments","events_url":"https://api.github.com/repos/rails/rails/issues/18573/events","html_url":"https://github.com/rails/rails/issues/18573","id":54677403,"number":18573,"title":"autosave association fails when associated model has uniqueness validation","user":{"login":"fw42","id":2072686,"avatar_url":"https://avatars.githubusercontent.com/u/2072686?v=3","gravatar_id":"","url":"https://api.github.com/users/fw42","html_url":"https://github.com/fw42","followers_url":"https://api.github.com/users/fw42/followers","following_url":"https://api.github.com/users/fw42/following{/other_user}","gists_url":"https://api.github.com/users/fw42/gists{/gist_id}","starred_url":"https://api.github.com/users/fw42/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fw42/subscriptions","organizations_url":"https://api.github.com/users/fw42/orgs","repos_url":"https://api.github.com/users/fw42/repos","events_url":"https://api.github.com/users/fw42/events{/privacy}","received_events_url":"https://api.github.com/users/fw42/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1"}],"state":"closed","locked":false,"assignee":{"login":"sgrif","id":1529387,"avatar_url":"https://avatars.githubusercontent.com/u/1529387?v=3","gravatar_id":"","url":"https://api.github.com/users/sgrif","html_url":"https://github.com/sgrif","followers_url":"https://api.github.com/users/sgrif/followers","following_url":"https://api.github.com/users/sgrif/following{/other_user}","gists_url":"https://api.github.com/users/sgrif/gists{/gist_id}","starred_url":"https://api.github.com/users/sgrif/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sgrif/subscriptions","organizations_url":"https://api.github.com/users/sgrif/orgs","repos_url":"https://api.github.com/users/sgrif/repos","events_url":"https://api.github.com/users/sgrif/events{/privacy}","received_events_url":"https://api.github.com/users/sgrif/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/rails/rails/milestones/34","labels_url":"https://api.github.com/repos/rails/rails/milestones/34/labels","id":665192,"number":34,"title":"5.0.0","description":null,"creator":{"login":"rafaelfranca","id":47848,"avatar_url":"https://avatars.githubusercontent.com/u/47848?v=3","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},"open_issues":92,"closed_issues":55,"state":"open","created_at":"2014-05-21T00:58:22Z","updated_at":"2015-02-02T19:13:20Z","due_on":null,"closed_at":null},"comments":11,"created_at":"2015-01-17T22:14:14Z","updated_at":"2015-02-02T19:35:56Z","closed_at":"2015-02-02T19:13:20Z","body":"I think https://github.com/rails/rails/pull/10417 didn't completely fix the issue I believe it was meant to fix.\r\n\r\nSee https://github.com/rails/rails/pull/10417#issuecomment-70385369.\r\n\r\nIf the associated model does not only have a uniqueness index but also a uniqueness validation, then saving the parent will still fail. I believe the reason is that ```validate_collection_association``` is called **before** any records are destroyed (i.e. even though the records that were marked for destruction are not validated, the new record is invalid because the old one still exists in the database at that time (```save_collection_association``` didn't run yet)). Similar problems can occur for any type of validation that considers records other than the one that is being validated.\r\n\r\nShitty workaround for now is to ```has_many ... autosave: true, validate: false``` on the parent model.\r\n\r\n@arthurnn @jholton"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/72522020","html_url":"https://github.com/rails/rails/issues/18573#issuecomment-72522020","issue_url":"https://api.github.com/repos/rails/rails/issues/18573","id":72522020,"user":{"login":"fw42","id":2072686,"avatar_url":"https://avatars.githubusercontent.com/u/2072686?v=3","gravatar_id":"","url":"https://api.github.com/users/fw42","html_url":"https://github.com/fw42","followers_url":"https://api.github.com/users/fw42/followers","following_url":"https://api.github.com/users/fw42/following{/other_user}","gists_url":"https://api.github.com/users/fw42/gists{/gist_id}","starred_url":"https://api.github.com/users/fw42/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fw42/subscriptions","organizations_url":"https://api.github.com/users/fw42/orgs","repos_url":"https://api.github.com/users/fw42/repos","events_url":"https://api.github.com/users/fw42/events{/privacy}","received_events_url":"https://api.github.com/users/fw42/received_events","type":"User","site_admin":false},"created_at":"2015-02-02T19:35:56Z","updated_at":"2015-02-02T19:35:56Z","body":"ok fair enough, thanks anyway"}}