Fernando P. wrote:
Hi,
In my views I often find myself doing:
<%= user.login %>
These get scattered all over the application. Now imagine I change the
user’s login to name. What happens? It blows in my face.
No. Remember that user.login is not an instance variable accessor, even
though it looks like one. It’s a method call.
So if you really wanted to, you could do
class User
def login
@name
end
end
Sure tests can
catch it, but writing tests for that purpose is too painful.
You’re not writing tests “for that purpose”. Rather, you write tests
that exercise as much of your code as possible, then let them tell you
when things break.
[…]
One should never access object’s attributes like this in the view,
Says who? Again, this is not really getting the model object’s
attributes – rather, it’s calling a method on the model object.
Whether the object happens to have an instance variable with the same
name as the method makes no difference to the view and is an irrelevant
implementation detail.
and
yet we see this in almost all tutorials, books and what have you.
Because there’s really no other way to do it, short of assigning every
model method call to a separate controller variable – and that’s just
plain silly.
It’s
now hard for me to get rid of that bad habit, and I don’t even know how.
Because of Ruby’s complete hiding of instance variables, I think it’s
not a bad habit. Don’t worry about it.
Best,
Marnen Laibow-Koser
http://www.marnen.org
[email protected]