{"action":"created","issue":{"url":"https://api.github.com/repos/thoughtbot/factory_girl/issues/735","labels_url":"https://api.github.com/repos/thoughtbot/factory_girl/issues/735/labels{/name}","comments_url":"https://api.github.com/repos/thoughtbot/factory_girl/issues/735/comments","events_url":"https://api.github.com/repos/thoughtbot/factory_girl/issues/735/events","html_url":"https://github.com/thoughtbot/factory_girl/issues/735","id":53425847,"number":735,"title":"belongs_to and validates :presence validations failing","user":{"login":"benwalsh","id":14017,"avatar_url":"https://avatars.githubusercontent.com/u/14017?v=3","gravatar_id":"","url":"https://api.github.com/users/benwalsh","html_url":"https://github.com/benwalsh","followers_url":"https://api.github.com/users/benwalsh/followers","following_url":"https://api.github.com/users/benwalsh/following{/other_user}","gists_url":"https://api.github.com/users/benwalsh/gists{/gist_id}","starred_url":"https://api.github.com/users/benwalsh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benwalsh/subscriptions","organizations_url":"https://api.github.com/users/benwalsh/orgs","repos_url":"https://api.github.com/users/benwalsh/repos","events_url":"https://api.github.com/users/benwalsh/events{/privacy}","received_events_url":"https://api.github.com/users/benwalsh/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-01-05T18:19:58Z","updated_at":"2015-11-13T21:38:38Z","closed_at":"2015-01-05T23:01:29Z","body":"After upgrading from FactoryGirl 2.5.2 to 4.5.0, validations for belongs_to associations fail.\r\n\r\nThe two model files:\r\n\r\n```\r\nclass User < ActiveRecord::Base    \r\n  ...\r\n  has_many :things\r\n  ...\r\nend\r\n\r\nclass Thing < ActiveRecord::Base\r\n  ...\r\n  belongs_to :user   \r\n  validates_presence_of :user\r\n  ...\r\nend\r\n```\r\n\r\nThe factory:\r\n```\r\nFactoryGirl.define\r\n  factory :thing do\r\n    association :user      \r\n    ...\r\n  end\r\nend\r\n```\r\nCreating a new thing always fails saying `User must be provided`. When I step into the code, the foreign key is set correctly, the object exists, but the association is not recognized:\r\n\r\n```\r\n>> user = create(:user)\r\n  ( returns saved \"user\" object )\r\n>> user.id\r\n1\r\n>> thing = build(:thing, user: user)\r\n  ( returns new \"thing\" object )\r\n>> thing.valid?\r\nfalse\r\n>> thing.user_id\r\n1\r\n>> User.find(1).present?\r\ntrue\r\n>> thing.user\r\nnil\r\n```\r\n\r\nI've tried all sorts of combinations and variations on this. I've tried using `after(:build)` blocks to set the user and using different FactoryGirl syntax. I've tried just having a plain user instead of the association. But it always comes down to this problem -- there is an associated key, and it is correct, but not an associated object (even though that object exists and is saved).\r\n\r\nEnvironment:\r\n\r\n```\r\nruby 2.1.5\r\n\r\nfactory_girl 4.5.0\r\nfactory_girl_rails 4.5.0\r\nrails 4.2.0\r\nrspec 3.1.0\r\n```"},"comment":{"url":"https://api.github.com/repos/thoughtbot/factory_girl/issues/comments/156563769","html_url":"https://github.com/thoughtbot/factory_girl/issues/735#issuecomment-156563769","issue_url":"https://api.github.com/repos/thoughtbot/factory_girl/issues/735","id":156563769,"user":{"login":"md5","id":76367,"avatar_url":"https://avatars.githubusercontent.com/u/76367?v=3","gravatar_id":"","url":"https://api.github.com/users/md5","html_url":"https://github.com/md5","followers_url":"https://api.github.com/users/md5/followers","following_url":"https://api.github.com/users/md5/following{/other_user}","gists_url":"https://api.github.com/users/md5/gists{/gist_id}","starred_url":"https://api.github.com/users/md5/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/md5/subscriptions","organizations_url":"https://api.github.com/users/md5/orgs","repos_url":"https://api.github.com/users/md5/repos","events_url":"https://api.github.com/users/md5/events{/privacy}","received_events_url":"https://api.github.com/users/md5/received_events","type":"User","site_admin":false},"created_at":"2015-11-13T21:38:38Z","updated_at":"2015-11-13T21:38:38Z","body":"I tracked this down to user error in our case.\r\n\r\nThere was a factory that wasn't creating one of the required `belongs_to` associations. The spec in question was passing in an associated object as an override to `create_list`, but another code path was creating its own instance without the override and failing the validation. Since this problem probably isn't relevant to the issue you were seeing, it's not worth the time for me to extract it into a demonstrable test case."}}