Skip to content

Question/Request: ability to access original arguments from within attribute blocks #1741

@andrewhavens

Description

@andrewhavens

I'm trying to figure out how to solve an issue with circular dependencies. For example, I want to create a factory that accepts one of three associations. Whichever association is not provided would be created and associated with the one that was provided.

# pseudo example
factory :person do
  parent { create :parent }
  child { create :child, parent: parent }
  something { create :something, parent: parent }
end

# usage
create(:person, parent: parent)
create(:person, child: child)
create(:person, something: something)

The problem I'm running into is not knowing which of these values was passed originally in the call to create. It's also valid to pass nil in order to instantiate the model with a missing association.

I've tried doing this in after(:build), but I have no way of knowing if the association was nil or if it was created because it wasn't specified.

If there was some way to access the original arguments, then I could know which one was provided. Is there already a way to do this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions