Active Record, changes from.. to..

Hi,

I’m thinking about a method that responds true or false for the changes of attributes from a value to another to trace a little bit the status of an AR object.

Ex.

class Book < ActiveRecord::Base

STATUS_DELIVER = [0,1]

before_save :send_mail_to_customer

def send_mail_to_customer

UserMailer.send_mail_to_customer if self.changes[‘status’] == [0, 1]

end

end

It’ a simple case but when complexity grows up a method like

def send_mail_to_customer

UserMailer.send_mail_to_customer if self.status.changes(from: 0, to: 1)

end

Maybe is more mantenible and readable.

I believe you can do this already with ActiveModel::Dirty:

def send_mail_to_customer

UserMailer.send_mail_to_customer if status_change == [0, 1]

end

This feature "from" and "to" was interesting, I extracted this to a gem

https://github.com/YasuOza/activemodel-attribute_changed_specification/

I wish this gem makes your happy.

Very nice! We coult try to made a Pr to Rails…