Ramy
Ramy

Reputation: 21261

how to capture the time a field is updated

I'm aware of updated_at and created_ate in rails. But what I'm interested in is the ability to update a field within a model when another field is updated. Here's what I've tried:

in my model:

protected

  def update_email_sent_on_date
    if self.send_to_changed?
      self.date_email_delivered = DateTime.now
    end
  end

and in the one place in my code that updates the field in question:

  distribution.send(:update_email_sent_on_date)

the problem is, this doesn't seem to be doing anything to my db table at all. I even tried removed the check on "send_to" but still nothing.

what am I doing wrong?

Upvotes: 0

Views: 62

Answers (1)

Batkins
Batkins

Reputation: 5706

You're not saving it after you make the change.

Change the method to this:

def update_email_sent_on_date
  if send_to_changed?
    self.date_email_delivered = DateTime.now
    save
  end
end

Or save the model after calling it like so:

distribution.update_email_sent_on_date
distribution.save

Upvotes: 1

Related Questions