On Monday, January 13, 2014 6:00:09 PM UTC-8, Andrew Hacking wrote: > > 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, delaying validation without also saving associated objects when the parent object is saved sounds wrong to me. By the time you implement that correctly, you've basically reimplemented nested attributes. In addition, you need to understand that the *_hook methods (e.g. after_save_hook) are not core methods, they are implemented by another plugin called instance_hooks. You'd basically need to reimplement that in core as well. As I mentioned earlier, you can't easily get nested_attributes features without reimplementing most of the plugin. > 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. > Assuming you are using nested_attributes, after you fix the foreign key validation issue, wouldn't that work in terms of accumulating model validation errors before saving? If not, could you provide some example code showing why it doesn't work for your use case? Thanks, Jeremy -- 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.
