ʻAttr_accessor` that came out before I didn't understand much so I looked it up
--Role of ʻattr_accessor --Reason for doing so without being aware of ʻattr_accessor
(Thanks to Rails)
I read two lines and wanted to abandon the rest
The remember_digest attribute has already been added to the User model, but the remember_token attribute has not yet been added. Therefore, it is necessary to make the token accessible using the user.remember_token method and implement the token without storing it in the database. Therefore, we will solve this by the same method as the password implementation performed in 6.3. At that time, I used the virtual password attribute and the secure password_digest attribute on the database. The virtual password attribute was created automatically with the has_secure_password method, but this time you have to write the remember_token code yourself. To implement this, we'll use the attr_accessor mentioned in 4.4.5 to create a "virtual" attribute. (Rails Tutorial 6th Edition)
The remember_digest attribute has already been added to the User model, but the remember_token attribute has not yet been added.
Existing attributes in the User model
User.attribute_names
=> ["id", "name", "email", "created_at", "updated_at", "password_digest", "remember_digest"]
There is no remember_token attribute
Used to define readable and writable attributes
By setting ʻattr_accessor: remember_token Define the remember_token attribute for the User class object ʻUser.remember_token
and refer to attributes
ʻUser.remember_token = assigning an attribute as "token"
Will be able to
I referred to the following What is Ruby's attr_accessor? [Japanese translation]
or ʻattr_accessor: email
I've been using ʻuser.name, ʻuser.email
so far
I had never been aware of ʻattr_accessor`
About this To those who entered from Rails [attr_accessor]?
In the process of rails generate model ...
and migration
Rails seemed to do the same thing without permission
Therefore, it is necessary to make the token accessible using the user.remember_token method and implement it without storing the token in the database. ... To implement this, we'll use the attr_accessor mentioned in 4.4.5 to create a "virtual" attribute.
remember_token is not saved in DB
In other words, without using rails generate model ...
or migration
I understand that I use ʻattr_accessor` because I want to add a new attribute
By the way ʻAttr_accessor: remember_token` is defined
User.attribute_names
=> ["id", "name", "email", "created_at", "updated_at", "password_digest", "remember_digest"]
And "remember_token" are not included as attributes of the object
user.remember_token = "token"
=> "token"
>> user.remember_token
=> "token"
Can be treated like an object attribute Is this the "" virtual "attribute"?