Skip to content

Instantly share code, notes, and snippets.

@Roman2K
Created October 15, 2008 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Roman2K/16852 to your computer and use it in GitHub Desktop.
Save Roman2K/16852 to your computer and use it in GitHub Desktop.
Test helper for painless record creation
# Test helper for painless record creation. Details at:
# http://roman.flucti.com/painless-record-creation-with-activerecord
#
def insert!(model, attributes={})
attributes = attributes.stringify_keys
begin
record = model.new(attributes)
def record.callback(*args)
# inhibit all callbacks
end
record.save(false)
rescue ActiveRecord::StatementInvalid
if $!.message =~ /Column '(.+?)' cannot be null/
unless attributes.key?($1)
attributes[$1] = record.column_for_attribute($1).number? ? 0 : ""
retry
end
end
raise
end
return record
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment