Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>you could use the <a href="http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html" rel="nofollow noreferrer">ActiveRecord::Dirty</a> module, which allows you to track unsaved changes.</p> <p>E.g.</p> <pre><code>t1 = Ticket.first t1.some_attribute = some_new_value t1.changed? =&gt; true t1.some_attribute_changed? =&gt; true t1.some_attribute_was =&gt; old_value </code></pre> <p>So inside a before_update of before_create you should those (you can only check before the save!).</p> <p>A very nice place to gather all these methods is in a <a href="http://api.rubyonrails.org/classes/ActiveRecord/Observer.html" rel="nofollow noreferrer">Observer-class</a> TicketObserver, so you can seperate your "observer"-code from your actual model. </p> <p>E.g.</p> <pre><code>class TicketObserver &lt; ActiveRecord::Observer def before_update .. do some checking here .. end end </code></pre> <p>to enable the observer-class, you need to add this in your <code>environment.rb</code>:</p> <pre><code>config.active_record.observers = :ticket_observer </code></pre> <p>This should get you started :)</p> <p>What concerns the linked comments. If you do this:</p> <pre><code>new_comment = ticket.ticket_comments.build new_comment.new_record? =&gt; true ticket.comments.changed =&gt; true </code></pre> <p>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 :)</p> <p>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).</p> <p>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: </p> <pre><code>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 &lt;&lt; "comment changed" if c.changed? @logmsg &lt;&lt; "comment added" if c.new_record? end end end end def send_mail_if_needed if @logmsg.size &gt; 0 ..send mail.. end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload