Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 779905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:59:50+00:00 2026-05-14T19:59:50+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:59:51+00:00Added an answer on May 14, 2026 at 7:59 pm

    you could use the ActiveRecord::Dirty module, which allows you to track unsaved changes.

    E.g.

    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 a before_update of before_create you should those (you can only check before the save!).

    A very nice place to gather all these methods is in a Observer-class TicketObserver, so you can seperate your “observer”-code from your actual model.

    E.g.

    class TicketObserver < ActiveRecord::Observer
      def before_update
        .. do some checking here ..
      end
    end
    

    to enable the observer-class, you need to add this in your environment.rb:

    config.active_record.observers = :ticket_observer
    

    This should get you started 🙂

    What concerns the linked comments. If you do this:

    new_comment = ticket.ticket_comments.build
    new_comment.new_record? => true
    ticket.comments.changed => true
    

    So that would be exactly what you would need. Does that not work for you?
    Note again: you need to check this before saving, of course 🙂

    I imagine that you have to collect the data that has changed in a before_create or before_update, and in an after_update/create actually send the mail (because then you are sure it succeeded).

    Apparently it still is not clear. I will make it a bit more explicit. I would recommend using the TicketObserver class. But if you want to use the callback, it would be 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, Article and Post that both inherit from a base model
I have two models. We'll call them object A and object B. Their design
I have two models indexed for searching (User and Item). I'm trying to do
I have two models, Room and Image . Image is a generic model that
Can somebody explain me one thing. I have two methods in my controller :
I have a SharePoint List to which I'm adding new ListItems using the Client
I'm running into a problem where I have a simple add/edit form and using
Maybe I am just having a slow day, but for the life of me
ModelMultipleChoiceField doesn't select initial choices and I can't make the following fix (link below)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.