Guest User

Untitled

a guest
Feb 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. def insert!(model, attributes={})
  2. attributes = attributes.stringify_keys
  3. begin
  4. record = model.new {|r| r.send(:attributes=, attributes, false) }
  5. def record.callback(*args)
  6. # inhibit all callbacks
  7. end
  8. record.save(false)
  9. rescue ActiveRecord::StatementInvalid
  10. if $!.message =~ /Column '(.+?)' cannot be null/
  11. unless attributes.key?($1)
  12. attributes[$1] = record.column_for_attribute($1).number? ? 0 : ""
  13. retry
  14. end
  15. end
  16. raise
  17. end
  18. return record
  19. end
Add Comment
Please, Sign In to add comment