you can use ActiveRecord :: Dirty module, which allows you to track unsaved changes.
eg.
t1 = Ticket.first t1.some_attribute = some_new_value t1.changed? => true t1.some_attribute_changed? => true t1.some_attribute_was => old_value
So, inside before_update before_create you have to do this (you can only check before saving!).
A very nice place to collect all these methods is in the Observer-class TicketObserver, so you can separate your "observer" code from your real model.
eg.
class TicketObserver < ActiveRecord::Observer def before_update .. do some checking here .. end end
in order to include the observer class, you need to add this to your environment.rb :
config.active_record.observers = :ticket_observer
This should help you :)
Regarding related comments. If you do this:
new_comment = ticket.ticket_comments.build new_comment.new_record? => true ticket.comments.changed => true
So that will be exactly what you need. This does not work for you? Remember again: you need to check this before saving, of course :)
I assume that you need to collect data that has been changed in before_create or before_update, and really send mail in after_update / create (because then you are sure that it succeeded).
Apparently, this is not yet clear. I will make it more explicit. I would recommend using the TicketObserver class. But if you want to use a callback, it will be something like this:
class Ticked before_save :check_state after_save :send_mail_if_needed def check_state @logmsg="" if ticket_comments.changed # find the comment ticket_comments.each do |c| @logmsg << "comment changed" if c.changed? @logmsg << "comment added" if c.new_record? end end end end def send_mail_if_needed if @logmsg.size > 0 ..send mail.. end end