I know that I can write attr_accessor :tag_list to make a virtual attribute tag_list for an object in Rails. This allows there to be a tag_list attribute in forms for the object.
If I use attr_accessor :tag_list I can, in the model, perform actions on tag_list to pull and manipulate data from the form.
What I want to know is, instead of writing attr_accessor, how would I write a getter and setter that would replicate completely the default functionality of attr_accessor. EG:
def tag_list
#what goes here
end
FYI I have tried
def tag_list
@tag_list
end
This does NOT work.
attr_accessor
is a built-in Ruby method and has no special meaning in the context ActiveRecord.attr_accessor :tag_list
is basically equivalent to this code:In ActiveRecord models, however, it could be that you want something like this:
There is a slight difference: With the first method,
obj[:tag_list]
doesn't use the same storage as your getter and setter. With the latter, it does.Explanation of the getter/setter concept
In Ruby, the following two lines of code are equivalent
Both call the method
blabla
of the objectthing
and evaluate to the last expression evaluated within that method. This means, you also don't need areturn
statement in the case of the above getter method, because the method simply returns the last expression in the method (@tag_list
, the value of the instance variable).Also, those two lines of code are equivalent:
Both call the method
blabla=
of the objectthing
. The special name with the=
character can be used like any other method name.The fact that attributes, as they are sometimes called, are in fact plain methods, you can also use some special logic transformed on the values before returning or accepting them. Example: