From: Wilson Bilkovich Date: 2006-04-03T21:34:16+09:00 Subject: Re: self.some_attribute vs @some_attribute On 4/2/06, Fred <0bssel602@sneakemail.com> wrote: > Hello, > > I just finished reading the Agile book. Of course I forgot at least the > two thirds of what I read. I started reading the source code of Typo, to > see a real (and Rails) application. > > I stumbled upon this snippet of code: > > def crypt_unless_empty > if password(true).empty? > user = self.class.find(self.id) > self.password = user.password > else > write_attribute "password", self.class.sha1(password(true)) > @password = nil > end > end > > is there any practical reason to use sometimes self.password and > sometimes @password? I thought they were synonyms... > Rails is a bit different in this scenario than most other Ruby programs. The attributes in your model classes that are picked up at runtime from your database schema are not defined as regular methods. @blah = 'something' will not necessarily do what you expect to the value in the 'blah' column of your table. If you're using an accessor you defined yourself, self.something and @something are equivalent. If you're using an ActiveRecord attribute, you should exclusively use self.something or self[:something]. --Wilson.