{"action":"created","issue":{"url":"https://api.github.com/repos/paulelliott/fabrication/issues/293","repository_url":"https://api.github.com/repos/paulelliott/fabrication","labels_url":"https://api.github.com/repos/paulelliott/fabrication/issues/293/labels{/name}","comments_url":"https://api.github.com/repos/paulelliott/fabrication/issues/293/comments","events_url":"https://api.github.com/repos/paulelliott/fabrication/issues/293/events","html_url":"https://github.com/paulelliott/fabrication/issues/293","id":238832359,"number":293,"title":"fabricate failing to build interit values for nested associations through nested attributes","user":{"login":"ajays1991","id":9007764,"avatar_url":"https://avatars3.githubusercontent.com/u/9007764?v=3","gravatar_id":"","url":"https://api.github.com/users/ajays1991","html_url":"https://github.com/ajays1991","followers_url":"https://api.github.com/users/ajays1991/followers","following_url":"https://api.github.com/users/ajays1991/following{/other_user}","gists_url":"https://api.github.com/users/ajays1991/gists{/gist_id}","starred_url":"https://api.github.com/users/ajays1991/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ajays1991/subscriptions","organizations_url":"https://api.github.com/users/ajays1991/orgs","repos_url":"https://api.github.com/users/ajays1991/repos","events_url":"https://api.github.com/users/ajays1991/events{/privacy}","received_events_url":"https://api.github.com/users/ajays1991/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-06-27T12:22:16Z","updated_at":"2017-06-27T13:30:04Z","closed_at":null,"body":"Hi,\r\n\r\nI observed a weird behaviour with fabrication.\r\n\r\nFollowings are my models\r\n```\r\n\r\n# frozen_string_literal: true\r\n\r\nclass Lead < ApplicationRecord\r\n  accept_nested_attributes_for :followers\r\n  has_many :followings, dependent: :destroy\r\n  has_many :followers, as: :followable, through: :followings, source: :followable, source_type: 'Lead'\r\nend\r\n\r\nclass Pipeline < ApplicationRecord\r\n  accept_nested_attributes_for :followers\r\n  has_many :followings, dependent: :destroy\r\n  has_many :followers, as: :followable, through: :followings, source: :followable, source_type: 'Lead'\r\nend\r\n\r\nclass Followings < ApplicationRecord\r\n  inherits_from :followable, attr: :account_id\r\n  belongs_to :followable, polymorphic: true\r\nend\r\n\r\n```\r\n\r\nModel Fabricators\r\n```\r\n\r\n# frozen_string_literal: true\r\n\r\nFabricator(:lead) do\r\n  max_budget   { Faker::Number.number(8) }\r\n  min_budget   { Faker::Number.number(5) }\r\n  requirements { Faker::ChuckNorris.fact }\r\nend\r\n\r\n# frozen_string_literal: true\r\n\r\nFabricator(:following) do\r\n  user    nil\r\nend\r\n\r\n```\r\n\r\nWhen i try to fabricate lead with followers, this fails\r\n```\r\n\r\n @lead =\r\n        @account.fabricate(\r\n          :lead,\r\n          pipeline: @pipeline,\r\n          target: @account.fabricate(:contact),\r\n          status: @status_old,\r\n          assignee: @user,\r\n          followers: [@account.owner]\r\n        )\r\n```\r\nwith the following error.\r\n`\r\n ActiveRecord::RecordInvalid: Validation failed: Followings is invalid` \r\n\r\nas it now getting the account_id from parent(inherits_from). I debug it and also found that followable_id is also nil. whereas the same is working fine through controllers and model create attributes.\r\n\r\n```\r\n\r\nlead = account.leads.create({\r\n    target_attributes: {\r\n    name:'ajay', email:'dsads.com'\r\n    }, \r\n    target_type: 'Contact',\r\n    pipeline: pipeline,\r\n    follower_ids: [account.owner.id]\r\n    })\r\n```\r\n\r\nThis points me different execution behaviour for models through fabricator or normal.\r\n\r\nThanks\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/paulelliott/fabrication/issues/comments/311358328","html_url":"https://github.com/paulelliott/fabrication/issues/293#issuecomment-311358328","issue_url":"https://api.github.com/repos/paulelliott/fabrication/issues/293","id":311358328,"user":{"login":"paulelliott","id":58612,"avatar_url":"https://avatars0.githubusercontent.com/u/58612?v=3","gravatar_id":"","url":"https://api.github.com/users/paulelliott","html_url":"https://github.com/paulelliott","followers_url":"https://api.github.com/users/paulelliott/followers","following_url":"https://api.github.com/users/paulelliott/following{/other_user}","gists_url":"https://api.github.com/users/paulelliott/gists{/gist_id}","starred_url":"https://api.github.com/users/paulelliott/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paulelliott/subscriptions","organizations_url":"https://api.github.com/users/paulelliott/orgs","repos_url":"https://api.github.com/users/paulelliott/repos","events_url":"https://api.github.com/users/paulelliott/events{/privacy}","received_events_url":"https://api.github.com/users/paulelliott/received_events","type":"User","site_admin":false},"created_at":"2017-06-27T13:30:04Z","updated_at":"2017-06-27T13:30:04Z","body":"You are correct that the execution behavior is different. Fabrication is not creating everything using nested attributes as your code does through a controller. You have validations on the associations and the way you're invoking fabrication is causing objects to persist in an order your application code does not expect.\r\n\r\nYour controller logic is likely doing a `build` on each object while it creates the graph of objects to save then when you call `save` on the parent it persists the group all at once. You should play around with using `fabricate.build` on the child objects and I think you'll see better results."}}
{"actor":{"display_login":"paulelliott"}}