If a model object has an attribute named balance, you can use the index operator to obtain the value of this attribute. You can use a string or tag. Here we use a tag, for example:
Account [: balance]#=>Get value
Account [: balance] = 0.0#=>Set Value
However, this common code is not recommended. It is better to use ruby's access method:
Account. balance#=>Get value
Account. balance = 0.0#=>Set Value
Here, we use two methods to obtain the attribute value. Active Record performs an appropriate type conversion. For example, if the column in the database is a TimeStamp, we will get a Time object. If you want to get the original value of the attribute, add _ before_type_cast to the end of the access method, for example:
Account. balance_before_type_cast# => "123.4", a string
Account. release_date_before_type_cast# => "20050301"
Finally, you can use the private methods read_attribute and write_attribute of the Model. These two methods use Attribute names as parameters.