I'm fairly confused and would appreciate some help.
I'm going through the Michael Hartl tutorial and the most complicated aspect (for me) is simply understanding what I perceive to be inconsistencies in syntax (I know I'm wrong in this regard and it's just my perception).
I'm currently on chapter 8, however a simple example from earlier on:
def show
@user = User.find(params[:id])
end
And another:
def create
@user = User.new(user_params)
if @user.save
log_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
As far as I understand instance variables, they are supposed to be attributes of the object or instance of the class, eg, :name, :email, :password_digest, etc.
In these examples, we haven't written an initialize method for the class as we simply generated a migration with 4/5 columns (which correspond with the attributes specified); the columns in this migration are, as I understand, interpreted by Rails as attributes or instance variables of the instance of the class (the object?)
What then is @user or rather why is an instance variable used in this context? It isn't an attribute (eg, :name) of an instance of the class, but rather appears to be a reference, placeholder or representation of an instance of the class?
n.b. I understand (well, as far as beginners understand) what the code above does, ie, queries the User model - which is also called User - to retrieve a record, however I don't understand why this is being assigned to an instance variable or in what contexts to use instance variables, ie, I thought they were for specifying attributes for an instance of the class, not for operating as a placeholder or referencing an instance of the class (including its attributes).
EDIT: I think my confusion emanates from instance variables being used for both specifying attributes and for, as per Mark's definition below, 'operating as a container for all the attributes of a class.'
The above examples appear to use instance variables as a 'container for attributes', whereas other examples I've read use instance variables to store attribute values.
An example from earlier in the book:
def initialize(attributes = {})
@name = attributes[:name]
@email = attributes[:email]
end
I guess it's both?