Skip to content

Commit

Permalink
Raise ActiveRecord::RecordNotSaved if any of the saves fail (but they…
Browse files Browse the repository at this point in the history
… won't fail due to validation since saves are done without validation, so this guards against callbacks failing etc.)
  • Loading branch information
jonleighton committed Nov 5, 2010
1 parent be21369 commit 9fb59fb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@ Bug fixes:

* Update the primary key sequence in PostgreSQL tables after seeding data. This ensures that id conflicts do not occur when records are subsequently added to the table.

* Raise ActiveRecord::RecordNotSaved if any of the saves fail (but they won't fail due to validation since saves are done without validation, so this guards against callbacks failing etc.)

Version 2.0.0
-------------

Expand Down
2 changes: 1 addition & 1 deletion lib/seed-fu/seeder.rb
Expand Up @@ -65,7 +65,7 @@ def seed_record(data)
puts " - #{@model_class} #{data.inspect}" unless @options[:quiet]

record.send(:attributes=, data, false)
record.save(:validate => false)
record.save(:validate => false) || raise(ActiveRecord::RecordNotSaved)
record
end

Expand Down
4 changes: 4 additions & 0 deletions spec/seeder_spec.rb
Expand Up @@ -137,4 +137,8 @@
lambda { SeededModel.create!(:title => "Bla") }.should_not raise_error
end
end

it "should raise an ActiveRecord::RecordNotSaved exception if any records fail to save" do
lambda { SeededModel.seed(:fail_to_save => true, :title => "Foo") }.should raise_error(ActiveRecord::RecordNotSaved)
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -23,6 +23,9 @@
class SeededModel < ActiveRecord::Base
validates_presence_of :title
attr_protected :first_name
attr_accessor :fail_to_save

before_save { false if fail_to_save }
end

RSpec.configure do |config|
Expand Down

0 comments on commit 9fb59fb

Please sign in to comment.