Rails accepts_nested_attributes_for callbacks

Posted by Rabbott on Stack Overflow See other posts from Stack Overflow or by Rabbott
Published on 2010-05-11T22:29:19Z Indexed on 2010/05/11 22:34 UTC
Read the original article Hit count: 311

I have two models Ticket and TicketComment, the TicketComment is a child of Ticket.

ticket.rb

class Ticket < ActiveRecord::Base
  has_many :ticket_comments, :dependent => :destroy, :order => 'created_at DESC'

  # allow the ticket comments to be created from within a ticket form
  accepts_nested_attributes_for :ticket_comments, :reject_if => proc { |attributes| attributes['comment'].blank? }
end

ticket_comment.rb

class TicketComment < ActiveRecord::Base
  belongs_to :ticket

  validates_presence_of :comment
end

What I want to do is mimic the functionality in Trac, where if a user makes a change to the ticket, and/or adds a comment, an email is sent to the people assigned to the ticket.

I want to use an after_update or after_save callback, so that I know the information was all saved before I send out emails.

How can I detect changes to the model (ticket.changes) as well as whether a new comment was created or not (ticket.comments) and send this update (x changes to y, user added comment 'text') in ONE email in a callback method?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about callbacks