What is the environment of a callback?
For example, in listing 6.20 of the standard Ruby on Rails Tutorial" (http://rails-4-0.railstutorial.org/book/modeling_users) it has:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
Where does "email" come from? are we in a function where email
is defined? or is actually @email
?
Also, is there an important reason why we use self.email
there rather than @email
(or just email
if it's the same as @email
)? or is that just stylistic, or pedagogical?
There are several things going on at once here:
self
in the block is the User
being saved. The block is also passed an argument but you don't have to use it.self
.self.email=
) needs an explicit receiver to distinguish it from a local variable.@email
) don't have anything to do with attributes in ActiveRecord models.self.email
or just email
if there is no email
local variable) and mutator methods (self.email=
) for ActiveRecord objects work with attributes, not instance variables.So this:
before_save { self.email = email.downcase } # (1)
could also be written as:
before_save { |user| user.email = user.email.downcase } # (2)
or even:
before_save { |user| user.send(:email=, user.send(:email).downcase) } # (3)
The main functional difference is that (2) requires the email
and email=
methods to be public.