Skip to content

Instantly share code, notes, and snippets.

@Roman2K
Created November 19, 2008 19:36
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/26647 to your computer and use it in GitHub Desktop.
Save Roman2K/26647 to your computer and use it in GitHub Desktop.
def insert!(model, attributes={})
attributes = attributes.stringify_keys
begin
record = model.new {|r| r.send(:attributes=, attributes, false) }
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