Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dirty and check if email attribute changed?

Last part of my project, Hopefully.

Need to check if user.email attribute changes. If it does, then need to tell mailchimp to change or add the email.

Looks like Dirty will do this, but have never used it before. How can I catch the change in a block, or pass it to a block, and then update the attribute?

like image 833
pcasa Avatar asked Jul 31 '10 17:07

pcasa


1 Answers

Using the ActiveRecord::Dirty module is pretty straightforward:

bob = User.find_by_email('[email protected]')
bob.changed?       # => false

bob.email = '[email protected]')
bob.changed?       # => true
bob.email_changed? # => true
bob.email_was      # => '[email protected]'
bob.email_change   # => ['[email protected]', '[email protected]']
bob.changed        # => ['email']
bob.changes        # => { 'email' => ['[email protected]', '[email protected]'] }
like image 135
John Topley Avatar answered Oct 06 '22 19:10

John Topley