Why you need attr_accessor and why you don't (Thanks to Rails): Rails Tutorial Memorandum--Chapter 9

ʻAttr_accessor` that came out before I didn't understand much so I looked it up

What i understood

--Role of ʻattr_accessor --Reason for doing so without being aware of ʻattr_accessor (Thanks to Rails)

This is the one that stumbled

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)

Understand little by little

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

What is ʻattr_accessor`?

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]

I've never done ʻattr_accessor: name 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

The important point was "do not save to database"

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"?

Recommended Posts

Why you need attr_accessor and why you don't (Thanks to Rails): Rails Tutorial Memorandum--Chapter 9
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapters 4 and 5
Rails: You need to supply at least one validation error
Rails Tutorial Records and Memorandum # 0
Things to remember and concepts in the Ruby on Rails tutorial
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
Why you need a bundle exec
Until you introduce fonts to Rails
To you who absolutely need backtrace
Rails Tutorial record and memorandum # 1 "From installation to hello_app deployment + error handling"