Using `self` in Model methods
A friend recently asked me about this, and although I already knew it, I couldn't remember when I learned it, so I'm putting it in here now.When calling another method from within a Model instance method, you don't strictly need to preface it with
self
:
class Person
def status
‘admin’
end
def admin?
self.status == ‘admin’ ? true : false
end
end
This works exactly the same:
class Person
def status
‘admin’
end
def admin?
status == ‘admin’ ? true : false
end
end
In fact, I almost never use
self
anymore in Model methods, except for readability purposes.UPDATE: I neglected to mention the importance of using
self
when assigning Model attributes. See my new post for more explanation.