Rick DeNatale
2011-Apr-07 21:03 UTC
accepts_nested_attributes_for, validations, :inverse_of option on associations, and IdentityMap
This feels more like a question to ask the core list, forgive me if I trespass. I''ve been trying to help one of my cow orkers get through a sticky problem. He''s got a fairly complex nested form to update a model and its children. He''s started adding some validations, and we''ve encountered a number of issues: First he had a validation on one of the child models which needed to be conditional on an attribute in the parent model. We realized that on a save, the child was fetching the parent from the database, and so was seeing the old value in the parent. This led me to the discovery of the :inverse_of option on :belongs_to and :has_xxx associations. Adding this option to both ends of the associations in question fixed the problem of the validation not triggering, and that''s when we noticed a second problem. Lets say we enter a value for an attribute of one of the child models, and validation of the parent fails. Using the normal pattern of rendering the edit action when the update fails doesn''t show the ''new'' values entered for the attribute(s) of the child(ren). Part of the problem is because active record now has this normally nice feature of deferring sequel queries until the data is needed, which, if you''re not careful, causes rendering of the edit view to refetch the data. It feels like we are working too hard and changing too much controller code to fix this, and not succeeding all that well to boot. It also occurs to me that the IdentityMap stuff now in Edge might be the solution or part of the solution to this problem. As I understand it this will prevent multiple instantiations of the same AR object in a given thread. What we really want is for the in-memory object graph to be preserved and keep the attributes from being overwritten by queries. I haven''t dug through the InstanceMap code enough yet to see if that is the case. I''m loath to move to edge just to try this. Any advice from the core-savvy guys. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Reasonably Related Threads
- Route globbing in Rails 3 beta 3
- Anyone using the rspec textmate bundle with Ruby 1.9.1
- inverse_of breaks for unsaved child associations during save of parent
- Any pioneers who've configured passenger to support some apps running in 1.8.x others in 1.9?
- ri_cal 0.7.0 Released