Currently, the nested attributes plugin doesn't add new associated
objects to the cached association array explicitly.  It will do so in
the after_save callback when it calls the association's add_ method,
but that will only happen if the object is saved successfully.

This is causing me some issues in the application I'm currently
developing, which I've worked around by changing the internal methods
to return more appropriate values, and then overriding one of them in
my model class.

The general use case for the nested_attributes plugin is a web form
that has both the main object and  dependent associated objects
displayed in the same form.  If you update the form and not everything
is valid, there should be no changes and you generally want to display
the exact same form to the user so they can fix the problems.  That's
problematic with the current implementation, as the object won't be
added to the cached association array, so if you are iterating over
the cached association array to display associated records in the
form, the newly added records won't be there.  I also need it in my
app because the attributes of the current model object affect the
validations required of some of the dependent associated objects, so I
need access to new associated records inside the main object's
validate method.

The solution to this is to add the new object to the cached
association array as soon as the nested attributes _attributes= method
is called.  The only current negative side effect that I'm aware of is
that after saving there will be two copies of the associated object in
the cached association (if the current model object is not a new
record).  That can be fixed by checking that the cached association
array doesn't already include the object, which we probably should be
doing anyway (we currently do it for adding to reciprocal
associations, but not for regular associations).

Unless someone objects with reasons why this is not a good idea, I
plan to make this change before the release of 3.6.0.

Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to