Self In Rails Models
Join the DZone community and get the full member experience.
Join For FreeFor assignment self is necessary. This clarifies that we assign an attribute and not a local variable.
class User < ActiveRecord::Base
def set_first_name_to_oliver
self.first_name = 'oliver'
end
end
For access self is not necessary and we should leave it away.
class User < ActiveRecord::Base
def full_name
"#{first_name} #{last_name}"
end
end
More:
- when-to-use-self-in-ruby-rails-models [themomorohoax.com]
- use-self-explicitly [rubyfleebie.com]
Self (programming language)
Opinions expressed by DZone contributors are their own.
Comments