{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/32116","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/32116/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/32116/comments","events_url":"https://api.github.com/repos/rails/rails/issues/32116/events","html_url":"https://github.com/rails/rails/issues/32116","id":300264409,"number":32116,"title":"Foreign key replacement in inverse association regression","user":{"login":"jfragoulis","id":1748736,"avatar_url":"https://avatars3.githubusercontent.com/u/1748736?v=4","gravatar_id":"","url":"https://api.github.com/users/jfragoulis","html_url":"https://github.com/jfragoulis","followers_url":"https://api.github.com/users/jfragoulis/followers","following_url":"https://api.github.com/users/jfragoulis/following{/other_user}","gists_url":"https://api.github.com/users/jfragoulis/gists{/gist_id}","starred_url":"https://api.github.com/users/jfragoulis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jfragoulis/subscriptions","organizations_url":"https://api.github.com/users/jfragoulis/orgs","repos_url":"https://api.github.com/users/jfragoulis/repos","events_url":"https://api.github.com/users/jfragoulis/events{/privacy}","received_events_url":"https://api.github.com/users/jfragoulis/received_events","type":"User","site_admin":false},"labels":[{"id":126910270,"url":"https://api.github.com/repos/rails/rails/labels/With%20reproduction%20steps","name":"With reproduction steps","color":"009800","default":false},{"id":107191,"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1","default":false},{"id":107186,"url":"https://api.github.com/repos/rails/rails/labels/regression","name":"regression","color":"e10c02","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/rails/rails/milestones/56","html_url":"https://github.com/rails/rails/milestone/56","labels_url":"https://api.github.com/repos/rails/rails/milestones/56/labels","id":2357255,"number":56,"title":"5.2.0","description":"","creator":{"login":"rafaelfranca","id":47848,"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},"open_issues":5,"closed_issues":55,"state":"open","created_at":"2017-03-01T20:55:32Z","updated_at":"2018-03-01T12:15:36Z","due_on":null,"closed_at":null},"comments":4,"created_at":"2018-02-26T14:43:26Z","updated_at":"2018-03-02T07:40:46Z","closed_at":"2018-03-01T12:15:36Z","author_association":"NONE","body":"### Steps to reproduce\r\n\r\nhttps://gist.github.com/jfragoulis/41a5529d26a8d9921b7aa54b6e17e001\r\n\r\n### Expected behavior\r\n\r\nTest does not blow up.\r\n\r\n### Actual behavior\r\n\r\nTest fails with validation error on `Post`.\r\n\r\n### System configuration\r\n**Rails version**: 5.2.0.rc1\r\n\r\n**Ruby version**: 2.3.3\r\n\r\nThis *regression* is traced to https://github.com/rails/rails/pull/31575.\r\nThe **expected** behavior is how it works in latest stable.\r\n\r\nEDIT:\r\n\r\nThe problem lies with the fact that when saving a parent object, its associations go through validation as well before the parent is actually saved. This is fine I guess generally I suppose, but in my use-case, I have a presence validation check on the fk (see gist) which means that since the #31575 sets the fk to `nil`, the validation fails.\r\n"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/369846220","html_url":"https://github.com/rails/rails/issues/32116#issuecomment-369846220","issue_url":"https://api.github.com/repos/rails/rails/issues/32116","id":369846220,"user":{"login":"jfragoulis","id":1748736,"avatar_url":"https://avatars3.githubusercontent.com/u/1748736?v=4","gravatar_id":"","url":"https://api.github.com/users/jfragoulis","html_url":"https://github.com/jfragoulis","followers_url":"https://api.github.com/users/jfragoulis/followers","following_url":"https://api.github.com/users/jfragoulis/following{/other_user}","gists_url":"https://api.github.com/users/jfragoulis/gists{/gist_id}","starred_url":"https://api.github.com/users/jfragoulis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jfragoulis/subscriptions","organizations_url":"https://api.github.com/users/jfragoulis/orgs","repos_url":"https://api.github.com/users/jfragoulis/repos","events_url":"https://api.github.com/users/jfragoulis/events{/privacy}","received_events_url":"https://api.github.com/users/jfragoulis/received_events","type":"User","site_admin":false},"created_at":"2018-03-02T07:40:46Z","updated_at":"2018-03-02T07:40:46Z","author_association":"NONE","body":"Thank you both for your comments.\r\n\r\n@kamipo You are right, it does feel like a bug and it is a correct fix.\r\n\r\nI verified that removing the validation check and adding `required: true` works.\r\n\r\nMy comment is the following: The documentation is either not clear or wrong and I should probably open a PR for this.\r\n\r\nIn the documentation under `belongs_to` and `:required` in both 5.1 and 5.2 we read:\r\n\r\n> When set to true, the association will also have its presence validated. This will validate the association itself, not the id. You can use :inverse_of to avoid an extra query during validation. NOTE: **required is set to true by default and is deprecated**. If you don't want to have association presence validated, use optional: true.\r\n\r\nThat means that removing the validation should suffice, since it is required by default. That is not the case though. The default value is set through `ActiveRecord::Base.belongs_to_required_by_default` which [is set to `false`](https://github.com/rails/rails/blob/master/activerecord/lib/active_record/core.rb#L126). My console says it is `nil` which is really weird:\r\n\r\n```\r\n[1] pry(main)> ActiveRecord::Base.belongs_to_required_by_default\r\n=> nil\r\n```\r\nThe [configure page](http://guides.rubyonrails.org/configuring.html) simply describes it, and the [upgrading page](http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#active-record-belongs-to-required-by-default-option) which says:\r\n\r\n> This default will be automatically configured in new applications\r\n\r\nThis is vague. My guess for what it means is that the new app generator will put it in your initializers or some config file. Which does not make it `true` by default as mentioned in the docs. True by default means, true in the framework's default.\r\n\r\nWhat do you think?\r\n"}}
{"actor":{"display_login":"jfragoulis"}}