Dirty and check if the email attribute has changed?

The last part of my project, I hope.

You need to check if the user.email attribute is changing. If so, then you need to tell mailchimp to change or add a letter.

Dirty seems to do it, but has never used it before. How can I catch a change in a block or pass it to a block and then update the attribute?

+4
source share
2 answers

I recommend using the Rails Dirty methods:

if @user.email.changed? # ... end 

But you can also do:

 if @user.email != params[:user][:email] # ... end 
0
source

Using the ActiveRecord :: Dirty module is pretty simple:

 bob = User.find_by_email(' bob@example.org ') bob.changed? # => false bob.email = ' robert@example.org ') bob.changed? # => true bob.email_changed? # => true bob.email_was # => ' bob@example.org ' bob.email_change # => [' bob@example.org ', ' robert@example.org '] bob.changed # => ['email'] bob.changes # => { 'email' => [' bob@example.org ', ' robert@example.org '] } 
+9
source

All Articles