-1

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 ?

Lucky
  • 140
  • 6

2 Answers2

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 scope
  • a_user.email when you have specified a user
  • email when you are in the User class. This is valid for every method in the User class.
  • @email, the variable returned by the email function
  • attributes[: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