Lille
2010-Oct-28 00:13 UTC
trouble testing callbacks on update to model with nested attributes
Hi, I have a parent model like this: class Parent < ActiveRecord::Base belongs_to :grand_parent has_many :blond_children, :dependent => :destroy has_many :brunette_children, :dependent => :destroy has_many :dogs, :dependent => :destroy accepts_nested_attributes_for :blond_children, :brunette_children, :dogs, :allow_destroy => true ... end I can''t seem to get the attributes of the children to change with attempts like the following parent_instance.blond_children_attributes = {"1"=>{:_destroy=>true}, "2"=>{:_destroy=>true}} parent_instance.save parent_instance.reload.blond_children.should == <some new number reflecting deletion> I''ve also tried this with hashes and using #update_attributes on the parent_instance. I keep experimenting, but I can''t get expected results. I''m wondering if maybe the problems are: 1) on an update, all children and their attributes need to be explicitly mentioned, even if the value is "" 2) where should the parent and child ids appear in any attempts to update child attributes? I might guess some readers have banged their head on something like this in the past, any pointers? Thanks, Lille -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Lille
2010-Oct-28 15:57 UTC
Re: trouble testing callbacks on update to model with nested attributes
I solved my problem by departing from the guidance at http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html Instead of... parent_instance.reload.blond_children # this resulted in blond_children == nil ...I used... refreshed_parent_instance = Parent.find(:last) refreshed_parent_instance.blond_children.blah_blah_blah... It seems, then that the problem I encountered concerns some departure my association structure takes from the standard model presumed in the api, where a reload should be equivalent to a find. Lille On Oct 27, 8:13 pm, Lille <lille.pengu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have a parent model like this: > > class Parent < ActiveRecord::Base > belongs_to :grand_parent > has_many :blond_children, :dependent => :destroy > has_many :brunette_children, :dependent => :destroy > has_many :dogs, :dependent => :destroy > > accepts_nested_attributes_for :blond_children, :brunette_children, :dogs, > :allow_destroy => true > ... > end > > I can''t seem to get the attributes of the children to change with > attempts like the following > > parent_instance.blond_children_attributes = {"1"=>{:_destroy=>true}, > "2"=>{:_destroy=>true}} > parent_instance.save > parent_instance.reload.blond_children.should == <some new number > reflecting deletion> > > I''ve also tried this with hashes and using #update_attributes on the > parent_instance. > > I keep experimenting, but I can''t get expected results. I''m wondering > if maybe the problems are: > > 1) on an update, all children and their attributes need to be > explicitly mentioned, even if the value is "" > 2) where should the parent and child ids appear in any attempts to > update child attributes? > > I might guess some readers have banged their head on something like > this in the past, any pointers? > > Thanks, > > Lille-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.