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)
clazz = factory.build_class
factory_attributes = FactoryGirl.attributes_for(name)
attributes = factory_attributes.merge(attributes)
clazz.find_or_create_by(attributes, &block)
end
end
@abinoam

This comment has been minimized.

Copy link
Owner Author

abinoam commented Dec 5, 2014

This last revision makes it:

  • NOT work if there's no defined factory for a given name
  • Work with inherited defined factories, even if their names doesn't match the class names
@thewatts

This comment has been minimized.

Copy link

thewatts commented Dec 23, 2014

Thanks for this -

One thing I noticed, is that line #9 isn't actually using FactoryGirl to create the object.

I've made some adjustments here: https://gist.github.com/thewatts/dcc91ef40b144aff42ae

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.