How to initialize variables in ActiveRecord class?
Variables here is the variables that are outside the scope of database
such as:
class Product
attr_accessor :used
end
I want to initially assign @used initially to false, later if some person access the product, i will change @used to true
First i thought of putting @used=false in initialize method, but it does not get called.
Or if you want to use initialize approach you can define callback after_initialize
Using
attr_accessor_with_defaultwith an object literal (e.g.attr_accessor_with_default :used, false) is unsafe to use with any mutable objects. Specifically, it means that different instances of your class will have the same object as their default value. This is sort of like trying to use a class variable (@@my_var) where you want an instance variable (@my_var). If you want to use a mutable object (e.g. aString,Array, orHash), you must use the block syntax: