Skip to content
Browse files

Preserve nested attributes on validation failures for *_to_one associ…



a = => 'Al', :artist_attributes => {:name => 'Ar'})

If the save fails either the Artist or Album validations, we want == 'Ar', so we can re-render the form and present
the user's input for correction. Previously, a.artist returned nil.
  • Loading branch information...
1 parent a5d8dd2 commit f7e17b34be4e32ae270be251a6422e0f132a3b5f John Firebaugh committed
Showing with 3 additions and 0 deletions.
  1. +1 −0 lib/sequel/plugins/nested_attributes.rb
  2. +2 −0 spec/extensions/nested_attributes_spec.rb
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)}
+ 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.
2 spec/extensions/nested_attributes_spec.rb
@@ -239,6 +239,8 @@ def validate
proc{}.should raise_error(Sequel::ValidationFailed)
a.errors.full_messages.should == ['artist name cannot be Ar']
@mods.should == []
+ # Should preserve attributes
+ == 'Ar'
it "should not attempt to validate nested attributes if the :validate=>false association option is used" do

0 comments on commit f7e17b3

Please sign in to comment.
Something went wrong with that request. Please try again.