Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Preserve nested attributes on validation failures for *_to_one associ…

…ations

Consider:

a = Album.new(:name => 'Al', :artist_attributes => {:name => 'Ar'})
a.save

If the save fails either the Artist or Album validations, we want
a.artist.name == 'Ar', so we can re-render the form and present
the user's input for correction. Previously, a.artist returned nil.
  • Loading branch information...
commit f7e17b34be4e32ae270be251a6422e0f132a3b5f 1 parent a5d8dd2
John Firebaugh authored
View
1  lib/sequel/plugins/nested_attributes.rb
@@ -91,6 +91,7 @@ def nested_attributes_create(reflection, attributes)
send(reflection[:name]) << obj
after_save_hook{send(reflection.add_method, obj)}
else
+ associations[reflection[:name]] = obj
# Don't need to validate the object twice if :validate association option is not false
# and don't want to validate it at all if it is false.
before_save_hook{send(reflection.setter_method, obj.save(:validate=>false))}
View
2  spec/extensions/nested_attributes_spec.rb
@@ -239,6 +239,8 @@ def validate
proc{a.save}.should raise_error(Sequel::ValidationFailed)
a.errors.full_messages.should == ['artist name cannot be Ar']
@mods.should == []
+ # Should preserve attributes
+ a.artist.name.should == 'Ar'
end
it "should not attempt to validate nested attributes if the :validate=>false association option is used" do
Please sign in to comment.
Something went wrong with that request. Please try again.