Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module FactoryGirl::Syntax::Methods
def find_or_create(name, attributes = {}, &block)
factory = FactoryGirl.factory_by_name(name)
klass = factory.build_class
factory_attributes = FactoryGirl.attributes_for(name)
attributes = factory_attributes.merge(attributes)
result = klass.find_by(attributes, &block)
if result
result
else
FactoryGirl.create(name, attributes, &block)
end
end
end
@renius

This comment has been minimized.

Copy link

@renius renius commented May 29, 2015

@samlehman

This comment has been minimized.

Copy link

@samlehman samlehman commented Dec 1, 2016

thanks, exactly what I needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.