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 ==

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs