In rails we can access db column through attributes rails provided, but can we change this ?
for example I have db with name column could I implement something like.
def name
"sir" + name
end
I tried it, but it result in stack overflow. There is a way to accomplish this.
more question, Is there any difference between name and self.name.
But avoid this practice. Use an additional getter/setter instead, also known as “virtual attribute”.
For more information read this answer: decorating an attribute in rails
In your case, there’s also a weird side effect. Each time the attribute is accessed, it appends the name. After 3 calls, you end up with
Indeed, you can do
but, seriously, don’t do this.