On 14/01/2014, at 9:42 AM, Jeremy Evans wrote:

> >>
> >> Perhaps the middle ground is to do it in core using a hook like 
> >> nested_attributes does so that model errors can be accumulated and 
> >> examined on an error, and allowing a special 
> >> raise_on_failure/raise_on_save_failure value of :none/:never as well as 
> >> preserving the current falsey/truthy value behaviour.
> >
> >
> > I'm not sure how this could be done without reimplementing much of 
> > nested_attributes in core (which I don't want to do), but I'll consider 
> > this approach it if could be done in a simple way.
> >
> 
> I'm happy with whatever you judge as appropriate. I like keeping core as 
> clean as possible so its easier to understand and not bloated with unwanted 
> concerns.
> 
> Let me be more clear: I'm not sure that the nested_attributes behavior could 
> be done in a simple way (without reimplementing much of the plugin).  
> However, if someone else comes up with a patch that does it in a simple way, 
> I will consider applying it. 
>  

I've been looking through the associations code and it appears that 
#add_associated_object, #set_associated_object and 
#set_one_to_one_associated_object could be changed/overridden in a plugin to 
defer validation and saving via an after_validation_hook/after_save_hook like 
nested does if the model is new, and possibly also controlling this behaviour 
by an option.

Specifically:
     1) if the parent object is new for #add_associated_object then defer via 
after_validation_hook/after_save hook like nested attributes.
     2) if the associated object is new for #set_associated_object and 
#set_one_to_one_associated_object then defer via 
before_validation_hook/before_save_hook.

I can see some care will be needed for reciprocal relationships in (2) to avoid 
callback recursion but besides that problem do you see any other obvious 
pitfalls before I embark upon such an approach?

> Well, it depends on where the exception is raised, definitely.  
> Sequel::ValidationFailed is designed so that it includes methods to get model 
> and the errors, and the nested_attributes plugin by default doesn't short 
> circuit validation (it validates all associated objects, then raises an 
> exception with all the validation errors).  And the default Sequel::Model 
> behavior with a single object is to get all validation errors on the object, 
> and then raise an exception if there are any errors.
> 

Yeah I don't have any problems with nested_attributes (aside from deferring 
foreign key validation and creating new model instances based on sti key), my 
problem stems from capturing exceptions and accumulating model validation 
errors for manual association operations which may occur in model 
callbacks/hooks or web app controller code.

Regards...Andrew

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to