Reputation: 329
I have a method
def call
user.password_reset_sent_at = Time.zone.now
user.save!
user.regenerate_password_reset_token
UserMailer.password_reset(user).deliver_later(queue: "low")
end
def user
@user = User.find_by_email(@params)
end
and I'm trying to reset the password_reset_token and the password_reset_sent_at
User::PasswordReset.new("[email protected]").call
I see the token updated but it does not update the password_reset_sent_at
Upvotes: 0
Views: 46
Reputation: 1767
Every occurrence of user
within call
is another invocation of the user
method, creating another User
object from the record in the database and storing it in @user
. The effect is similar to if you had written
def call
User.find_by_email(@params).password_sent_at = Time.zone.now
User.find_by_email(@params).save!
... etc ...
end
The changes you make to the first copy of the User record you retrieve, are never saved before you go and get a new copy.
I think the idiom you are aiming for involves defining user
like this:
def user
@user ||= User.find_by_email(@params)
end
Defined that way, User.find_by_email
will only be called once, and the result stored in @user
. Subsequent calls to user
will re-use the existing value of @user
, a technique called memoization.
Upvotes: 1