Topic: Model Confusion: self.name vs. @name

Hi,

I am new to Ruby and Rails and already programmed quite a bit, but I still don't get the difference.

If I have a model User that looks like this:

class User < ActiveRecord::Base
  def status
    if self.logged_in == nil
      "offline"
    else
      "online"
    end
  end

end

Do I have to use self.logged_in to access the logged_in field of user1 (with ID = XY) or can I use @logged_in?

What is the difference, it is both instance variables for me, but sometimes the @xyz is not working and I have to use self.xyz.

When do you have to use what? What do I use to write to the table?

Thanks a lot!

Re: Model Confusion: self.name vs. @name

self.name is a method call

It will only work if you have the following method defined:
def name
  @name
end

The confusion is probably that if you use the attr_reader shortcut:
attr_reader :name

it's basically doing the same as above.

If it's not there... self.name will give a NoMethodError