has_many association, nested models and callbacks
        Posted  
        
            by fl00r
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by fl00r
        
        
        
        Published on 2010-05-10T14:28:41Z
        Indexed on 
            2010/05/11
            11:44 UTC
        
        
        Read the original article
        Hit count: 331
        
Hi!
I've got model A and model Attach. I'm editing my A form with nested attributes for :attaches. And when I am deleting all attaches from A via accepts_nested_attributes_for how can I get after_update/after_save callbacks for all of my nested models? Problem is that when I am executing callbacks in model A they are executed right AFTER model A is updated and BEFORE model Attach is updated, so I can't, for example, know if there is NO ANY attaches after I delete them all :).
Look for example: my callback after_save :update_status won't work properly after I delete all of my attaches.
model A
  after_save :update_status
  has_many :attaches
  accepts_nested_attributes_for :attaches, :reject_if => proc { |attributes| attributes['file'].blank? }, :allow_destroy => true
  def update_status
    print "\n\nOUPS! bag is empty!\n\n" if self.attaches.empty?
  end
end
model Attach
  belongs_to A
end
I am using rails 3 beta
© Stack Overflow or respective owner