Home>
class User

↑ This is the content of the user model with the verification content entered in the name column and email column.
I can understand the verification itself, but in the second line before_save {self.email = email.downcace}
I don't understand the reason to use self.
Is it a rule to use self as the type of the callback method? (Should it be considered as something?)
Is this defined as a class method?
If so, why put a class method?

As a result of the investigation,
○ self here refers to the user object at the time the method is called
○ Class method is the method to apply to the class

I think it's a rudimentary content, but I hope you can teach me and hold it.
We apologize for the inconvenience, but thank you.

  • Answer # 1

    I don't understand the reason to use self.

    before_saveThe blocks inside areModel instanceIt runs in the context of. That is, thisselfRefers to the model instance.

    Andemail =If you substituteLocal variableSince the model will not be rewritten as it is assigned to, in order to call the setter of the modelselfIs required.

  • Answer # 2

    The result of the examination is correct.

    In such a case, self is not necessary in most cases, but if self is omitted (not only before_save), it may be mistaken for a "mere variable".
    I think that's because I didn't like it, so I added self just in case.