In PHP, I can set an attribute (that is not a column in database) to a model. E.g.(PHP code),
$user = new User;
$user->flag = true;
But in rails, when I set any attribute that doesn't exist in database, it will throw an error undefined method flag
. There is attr_accessor
method, but what will happen if I need about ten temp attributes?
All that attr_accessor does is add getter and setter methods which use an instance variable, eg this
will add these methods:
You can write these methods yourself if you want, and have them do something more interesting than just storing the value in an instance var, if you want.
As the guys explained,
attr_accessor
is just a quicksetter
andgetter
.We can set our Model attr_accessor on record initializing to be a Ruby#Hash for example using ActiveRecord#After_initilize method so we get more flexibility on temporarily storing values (idea credit to this answer).
Something like:
Now you could do:
If you need temp attributes you can add them to the singleton object.
This is expected because it's how ActiveRecord works by design. If you need to set arbitrary attributes, then you have to use a different kind of objects.
For example, Ruby provides a library called
OpenStruct
that allows you to create objects where you can assign arbitrary key/values. You may want to use such library and then convert the object into a corresponding ActiveRecord instance only if/when you need to save to the database.Don't try to model
ActiveRecord
to behave as you just described because it was simply not designed to behave in that way. That would be a cargo culting error from your current PHP knowledge.attr_accessor
creates "virtual" attributes in Rails -- they don't exist in the database, but are present in the model.As with attributes from the db,
attr_accessor
just creates a set ofsetter
&getter
(instance) methods in your class, which you can call & interact with when the class is initialized: