Difference between self and @

What is the difference between self.name and @name in case of a model class?

if I set @name = “ankur” inside a before_create filter then this does not work but self.name = “ankur” works.

self.name is a method call whereas @name = is an instance variable assignment. I prefer to use the setters wherever possibe.

See: Different ways of initializing variables in the initialize method - #2 by derekprior - Ruby on Rails - thoughtbot

1 Like

Oh. Thanks

And then there’s CoffeeScript, which is just different enough to trip you up when you have to context switch from Ruby…