Search code examples
ruby-on-rails-3observers

Rails 3 Observers and passing variables to method


I am integrating the MailChimp API into my app and am using a UserObserver to add the user to a MailChimp list on after_create, but I am having a slight problem with trying to update the user.

I allow the user to update their email address in the system which would be different in the MailChimp list, how could I pass the original email address to the before_update callback?


Solution

  • This is how I ended up doing it.

    I set a new attr_accessor in my UserModel

    attr_accessor :current_email
    attr_accessible :current_email
    

    And then in my Account Update Form, I set a hidden form field with the current_email

    <%= f.hidden_field :current_email, :value => current_user.email %>
    

    And the in my UserObserver I can access current_user with the user object being passed back.

      def after_update(user)
        ap user.current_email
      end
    

    Hope this helps someone else