Observer not picking state change in user

Hello,-

does anyone have a good idea of how to trace a fault in the following:

I have a User model (user.rb) with these methods:

def create_reset_code
    code = User.get_random_string(10) #working class method
    self.password_reset_code = code
    self.save(false)
    @reset = true
    #Notifier.deliver_reset_notification(self) #works from here
  end

  def recently_reset?
    @reset
  end

Also I have an Observer with this code:

class UserObserver < ActiveRecord::Observer
  def after_save(user)
    debugger
    puts "I observe " + user.username
    puts "Did " + user.username + " reset his password? " +
user.recently_reset?.to_s
    Notifier.deliver_reset_notification(user) if user.recently_reset?
  end
end

The problem is that the user.recently_reset? in the Observer returns
nil.

When I test def recently_reset? interactively in the console (testing
the User model), it works as intended. So the user model works correctly
and the Observer does, too (as evidenced by the debugger and the first
puts statement).

I tried to test the UserObserver interactively but get this:

obs = UserObserver.new

NoMethodError: private method `new' called for UserObserver:Class
  from (irb):1

Why does the Observer fail to read the user.recently_reset? correctly?

Thanks a lot,
Vahagn

The problem is that the user.recently_reset? in the Observer returns
nil.

When I test def recently_reset? interactively in the console (testing
the User model), it works as intended. So the user model works
correctly
and the Observer does, too (as evidenced by the debugger and the first
puts statement).

Well you set @reset to true after you've called save: at this point
all the callbacks and observers and so on have already run so it's too
late. You need to play with @reset before you call save.

Fred

Oh well. I just love those programming mistakes that make one appear
like a total n00b because they are so basic one doesn't even stop to
think about their possibility of occurring.

Thanks Fred, it's working now :slight_smile:

/ Vahagn