Created
November 19, 2008 19:36
-
-
Save Roman2K/26647 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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