Allow overriding validations - pass :perform_validations => false to save from create_or_update while skipping model validations.

This commit is contained in:
Mike Gunderloy 2009-11-26 11:09:31 -06:00
parent 272c102f95
commit 0b1c79c6f7
2 changed files with 3 additions and 3 deletions

View File

@ -10,14 +10,14 @@ class ActiveRecord::Base
# Returns the record.
def self.create_or_update(options = {})
id = options.delete(primary_key.to_sym)
validate = options.delete(:perform_validations) || true
validate = options.delete(:perform_validations){|k| true}
record = send("find_by_#{primary_key}", id) || new
record.id = id
record.attributes = options
if validate
record.save!
else
record.save!(false)
record.save(false)
end
record
end

View File

@ -49,7 +49,7 @@ class DbPopulateTest < Test::Unit::TestCase
def test_creates_new_record_without_validation
Customer.delete_all
Customer.create_or_update(:cust_id => 1, :name => "Me")
Customer.create_or_update(:cust_id => 1, :name => "Me", :perform_validations => false)
assert_equal Customer.count, 1
c = Customer.find(:first)
assert_equal c.name, "Me"