Ruby on Rails's model has an email attribute, so what's the difference between self.email: email and email?

recently read railstutorial .
the model generated through rails g model User name:string email:string has gradually become the following virtues with the tutorials:

class User < ApplicationRecord
  before_save { self.email = email.downcase }
  validates :name,  presence: true, length: { maximum: 50 }
  -sharpVALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
end

what is the meaning of self.email , email and : email in this code, and what"s the difference?
if you change validates: email,. to validates self.email,. after
, why do you report NoMethodError: NoMethodError: undefined method "email" ?

Mar.01,2021

  • before_save {self.email = email.downcase}

the self.email indicates the email attribute of the current object; the email of email.downcase means to call an email method ( ActiveRecord is automatically generated for you); the meaning of this line of code is: before a User instance object is saved ( before_save ), email is converted to lowercase;
your question is converted to lowercase.
answer: self is not used in most cases, and this is the only case where you need to display the use of self .

  • validates: email,. change to validates self.email,. after

validates is a class macro ( class macro ). The self in the class macro represents the current class object itself ( User ). The current class User object (class object itself) does not have a email method (the current class User has an instance method email , which is generated for you by ActiveRecord ). The
code means that before a User instance object save or update , you need to verify the email attribute; if you change it to validates self.email,. , it means to verify the email method of the User class object itself. Since the User class object itself does not have a email method, the NoMethodError

PS: suggests that if you study Ruby metaprogramming (the second edition of the Chinese version has already been published), you will have a more thorough understanding of the nature of ruby. If you think that many methods in Ruby do not know how to use, The Ruby Way: Solutions and Techniques in Ruby Programming recommended by DHH is also a high-level book. Pick book "Programming Ruby" and David Flanagan's "The Ruby Programming Language" are also two very good ruby complete solutions (although these two books are a little out of date, they have no effect on you);

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b369ac-2c033.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b369ac-2c033.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?