Skip to content

Instantly share code, notes, and snippets.

@abinoam
Forked from Aerlinger/fg_find_or_create.rb
Last active February 3, 2017 14:35
Show Gist options
  • Save abinoam/4ab40317ef9d1424d808 to your computer and use it in GitHub Desktop.
Save abinoam/4ab40317ef9d1424d808 to your computer and use it in GitHub Desktop.
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
@thewatts
Copy link

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