Suppose There is a model User with email attribute. I have check in some of the tutorials that we can use self.email and email alone. So what is the difference in both ?
Asked
Active
Viewed 364 times
-1
-
Post some code so that we know what you mean. – Sergio Tulentsev Mar 22 '13 at 11:20
-
They both are same in a instance method – Salil Mar 22 '13 at 11:22
-
Take a look at this post http://stackoverflow.com/questions/386115/to-use-self-or-not-in-rails All is explained there – Aleks Mar 22 '13 at 11:26
2 Answers
2
If you are in an instance method within the User model then either will work, but email
on its own is an implicit scope definition - meaning that the application will look for a local email
variable, then an email method/attribute. self.email
explicitly skips the search for a local variable.

Matt
- 13,948
- 6
- 44
- 68
1
You can access email from different ways when you are on the User class.
self.email
when you are in the User scopea_user.email
when you have specified a useremail
when you are in the User class. This is valid for every method in the User class.@email
, the variable returned by theemail
functionattributes[:email]
the ActiveRecord attribute.
All of this methods are automatically generated by the ActiveRecord model, you can see the doc for more details.

pierallard
- 3,326
- 3
- 21
- 48