{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/26661","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/26661/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/26661/comments","events_url":"https://api.github.com/repos/rails/rails/issues/26661/events","html_url":"https://github.com/rails/rails/issues/26661","id":180057417,"number":26661,"title":"Accessing ActiveRecord relation from ActiveModel::Validator mutates the collection","user":{"login":"kirs","id":522155,"avatar_url":"https://avatars.githubusercontent.com/u/522155?v=3","gravatar_id":"","url":"https://api.github.com/users/kirs","html_url":"https://github.com/kirs","followers_url":"https://api.github.com/users/kirs/followers","following_url":"https://api.github.com/users/kirs/following{/other_user}","gists_url":"https://api.github.com/users/kirs/gists{/gist_id}","starred_url":"https://api.github.com/users/kirs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kirs/subscriptions","organizations_url":"https://api.github.com/users/kirs/orgs","repos_url":"https://api.github.com/users/kirs/repos","events_url":"https://api.github.com/users/kirs/events{/privacy}","received_events_url":"https://api.github.com/users/kirs/received_events","type":"User","site_admin":false},"labels":[],"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},"assignees":[{"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/53","html_url":"https://github.com/rails/rails/milestones/5.0.1","labels_url":"https://api.github.com/repos/rails/rails/milestones/53/labels","id":1730927,"number":53,"title":"5.0.1","description":null,"creator":{"login":"jeremy","id":199,"avatar_url":"https://avatars.githubusercontent.com/u/199?v=3","gravatar_id":"","url":"https://api.github.com/users/jeremy","html_url":"https://github.com/jeremy","followers_url":"https://api.github.com/users/jeremy/followers","following_url":"https://api.github.com/users/jeremy/following{/other_user}","gists_url":"https://api.github.com/users/jeremy/gists{/gist_id}","starred_url":"https://api.github.com/users/jeremy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeremy/subscriptions","organizations_url":"https://api.github.com/users/jeremy/orgs","repos_url":"https://api.github.com/users/jeremy/repos","events_url":"https://api.github.com/users/jeremy/events{/privacy}","received_events_url":"https://api.github.com/users/jeremy/received_events","type":"User","site_admin":false},"open_issues":18,"closed_issues":34,"state":"open","created_at":"2016-04-26T02:02:53Z","updated_at":"2016-09-29T19:02:43Z","due_on":null,"closed_at":null},"comments":2,"created_at":"2016-09-29T14:00:22Z","updated_at":"2016-09-29T19:08:30Z","closed_at":"2016-09-29T19:02:43Z","body":"### Steps to reproduce\r\n\r\n```ruby\r\nrequire \"bundler/setup\"\r\nrequire 'active_record'\r\nrequire 'sqlite3'\r\n\r\nActiveRecord::Base.establish_connection(adapter: \"sqlite3\", database: \":memory:\")\r\nActiveRecord::Schema.define do\r\n  create_table :applied_gift_cards, force: true do |t|\r\n    t.references :checkout, null: false\r\n  end\r\n  create_table :checkouts, force: true do |t|\r\n  end\r\nend\r\n\r\nclass GiftCardValidator < ActiveModel::Validator\r\n  def validate(model)\r\n    puts \"count inside validator: #{model.checkout.applied_gift_cards.size}\"\r\n  end\r\nend\r\n\r\nclass Checkout < ActiveRecord::Base\r\n  has_many :applied_gift_cards\r\nend\r\n\r\nclass AppliedGiftCard < ActiveRecord::Base\r\n  belongs_to :checkout\r\n\r\n  validates :checkout, presence: true\r\n  validates_with GiftCardValidator\r\nend\r\n\r\ncheckout = Checkout.create!\r\ncheckout.applied_gift_cards.create!\r\ncheckout.applied_gift_cards.create!\r\n\r\nputs \"records created: #{checkout.applied_gift_cards.size}\"\r\ncheckout.reload\r\nputs \"records created: #{checkout.applied_gift_cards.size}\"\r\n```\r\n\r\n### Expected behavior\r\n\r\nIt's expected that after calling `create!` two times, the `checkout.applied_gift_cards.size` value will be `2`\r\n\r\n### Actual behavior\r\n\r\n```\r\ncount inside validator: 0\r\ncount inside validator: 0\r\nrecords created: 1\r\nrecords created: 2\r\n```\r\n\r\n### System configuration\r\n**Rails version**: master (`21ecf42730de2004a95d913038c1f417c24496be`)\r\n\r\n**Ruby version**: 2.3.0\r\n\r\nThe bug only affects >= 5.0. Rails 4.2 works fine:\r\n\r\n```\r\ncount inside validator: 0\r\ncount inside validator: 1\r\nrecords created: 2\r\nrecords created: 2\r\n```\r\n\r\n?r @sgrif \r\ncc @rafaelfranca "},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/250561980","html_url":"https://github.com/rails/rails/issues/26661#issuecomment-250561980","issue_url":"https://api.github.com/repos/rails/rails/issues/26661","id":250561980,"user":{"login":"kirs","id":522155,"avatar_url":"https://avatars.githubusercontent.com/u/522155?v=3","gravatar_id":"","url":"https://api.github.com/users/kirs","html_url":"https://github.com/kirs","followers_url":"https://api.github.com/users/kirs/followers","following_url":"https://api.github.com/users/kirs/following{/other_user}","gists_url":"https://api.github.com/users/kirs/gists{/gist_id}","starred_url":"https://api.github.com/users/kirs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kirs/subscriptions","organizations_url":"https://api.github.com/users/kirs/orgs","repos_url":"https://api.github.com/users/kirs/repos","events_url":"https://api.github.com/users/kirs/events{/privacy}","received_events_url":"https://api.github.com/users/kirs/received_events","type":"User","site_admin":false},"created_at":"2016-09-29T19:08:30Z","updated_at":"2016-09-29T19:08:30Z","body":"❤️ ❤️ ❤️ "}}
{"actor":{"display_login":"kirs"}}