I am watching Ruby On Rails Live Lessons videos and the author defined an Active Record User model. In the model, he made password_confirmation field part of the model. Is this standard practise in RoR? Seems weird to me to define it as part of the model…
For example, you don’t define email confirmation as part of the model or maybe username confirmation.
Is this normal/standard RoR practise?
You can add virtual attributes (attributes that are in-memory only. They’re not backed by the database) to AR models.
To the clients of the model, the attribute looks real, but it is not stored in the database.
In the example, the password_confirmation attribute is virtual.
Remember that there are two ways to think of a Rails User model:
Number 2 is the preferred way to build apps. It’s the idea behind the “Thick models, thin controllers” motto.
So it often makes lots of sense to add virtual attributes to a model to enable mass setting of attributes from an incoming form. The real and virtual attributes are then used by the model’s methods as appropriate.