# Total beginner asks a few questions

**URL:** <https://www.ruby-forum.com/t/total-beginner-asks-a-few-questions/90355>\
**Category:** Rails\
**Created:** [March 28, 2007, 2:34am UTC](https://www.ruby-forum.com/t/total-beginner-asks-a-few-questions/90355 "2007-03-28T02:34:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![running\_idiot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/898d66/32.png) [@running\_idiot](https://www.ruby-forum.com/u/running_idiot)\
**Post date:** [March 28, 2007, 2:34am UTC](https://www.ruby-forum.com/t/total-beginner-asks-a-few-questions/90355/1 "2007-03-28T02:34:38Z")

</div>

Hi there,

I am doing the “depot” application from Agile Web D. With  
Rails book - and there is a lot of copy-and-paste. And I have to say,  
as a beginner in both Ruby and Rails, I don’t understand quite a lot  
of things.

Let’s talk about this thing - there is a method called “add\_to\_cart”.  
I don’t understand much, what it’s doing… (it’s in class  
StoreController \< ApplicationController)

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
@cart.add\_product(product)  
redirect\_to(:action =\> ‘display\_cart’)  
end

So, I have a few questions.

1. what exactly is product? Why it’s not @product? Is it global or  
local variable?
2. what exactly is Product? I’m not sure, but I think its the only  
instance of Product class, right? Then why is the name of instance and  
name of the class the same?
3. What exactly is this params thing?
4. what exactly is :id? I don’t understand, in which cases you use  
colon and in which not - why I have to use it in Product.find, but not  
in add\_product?
5. what exactly does =\> mean? Why I can’t use ordinary = ?

Sorry if I ask too much, I just want to understand this language and  
this framework.

Kaja Bilek

---

<div class="post-metadata">

**Author:** ![running\_idiot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/898d66/32.png) [@running\_idiot](https://www.ruby-forum.com/u/running_idiot)\
**Post date:** [March 28, 2007, 3:19am UTC](https://www.ruby-forum.com/t/total-beginner-asks-a-few-questions/90355/2 "2007-03-28T03:19:43Z")

</div>

running idiot wrote:

> 1. what exactly is product? Why it’s not @product? Is it global or  
> local variable?  
> product is a local variable used to store the result of  
> Product.find(…)
> 2. what exactly is Product? I’m not sure, but I think its the only  
> instance of Product class, right? Then why is the name of instance and  
> name of the class the same?

Product.find(…) is calling the ‘Product’ _class_ method ‘find’ - it’s  
not an instance

> 1. What exactly is this params thing?

params is the parameter hash passed when the request is made, e.g. when  
you submit a form your data comes in through params - ditto when you  
make a GET request via an URL (/user/23 for example)

> 1. what exactly is :id? I don’t understand, in which cases you use  
> colon and in which not - why I have to use it in Product.find, but not  
> in add\_product?

the preceding colon signifies a symbol - there’s some good info on ruby  
symbols here =\>  
[http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols](http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols) &&  
here =\> [The Ruby\_Newbie Guide to Symbols](http://www.troubleshooters.com/codecorn/ruby/symbols.htm)

> 1. what exactly does =\> mean? Why I can’t use ordinary = ?

key=\>value, pretty common across different languages

> Sorry if I ask too much, I just want to understand this language and  
> this framework.

perhaps have a look @ [http://pine.fm/LearnToProgram/](http://pine.fm/LearnToProgram/) - a tasty little  
morsel that should help - not the most comprehensive answer but it’s 2am  
and i’ve been dipping the whiskey 😛

---

<div class="post-metadata">

**Author:** ![running\_idiot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/898d66/32.png) [@running\_idiot](https://www.ruby-forum.com/u/running_idiot)\
**Post date:** [March 28, 2007, 4:54am UTC](https://www.ruby-forum.com/t/total-beginner-asks-a-few-questions/90355/3 "2007-03-28T04:54:50Z")

</div>

I am also new and was getting a little lost (although I’ve learned a  
tremendous amount) with local variables and things that I percieved as  
variables.

So when does product refer to my product model? And what exactly is  
the difference between the local variable w/ @ and without. I thought  
there weren’t a whole lot of variables without the @.

---

<div class="post-metadata">

**Author:** ![running\_idiot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/898d66/32.png) [@running\_idiot](https://www.ruby-forum.com/u/running_idiot)\
**Post date:** [March 28, 2007, 10:19am UTC](https://www.ruby-forum.com/t/total-beginner-asks-a-few-questions/90355/4 "2007-03-28T10:19:30Z")

</div>

Hi Kaja,

I’d just like to say that if you’re a total beginner, copying and  
pasting is not what you should be doing. The best thing to do is to  
manually type out the examples as you read them. Why? Because you  
WILL make mistakes, and by making them, you must learn how to fix them  
to continue. You’ll learn a lot more a lot faster if you do this.

Skimming a book and pasting examples isn’t a good way to learn. Get  
your hands dirty! Making mistakes is a great way to learn 🙂

Cheers!  
OJ

---

<div class="post-metadata">

**Author:** ![running\_idiot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/898d66/32.png) [@running\_idiot](https://www.ruby-forum.com/u/running_idiot)\
**Post date:** [March 28, 2007, 5:06am UTC](https://www.ruby-forum.com/t/total-beginner-asks-a-few-questions/90355/5 "2007-03-28T05:06:52Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I am also new and was getting a little lost (although I’ve learned a  
> tremendous amount) with local variables and things that I percieved as  
> variables.
> 
> So when does product refer to my product model? And what exactly is  
> the difference between the local variable w/ @ and without. I thought  
> there weren’t a whole lot of variables without the @.

If I remember correctly, the convention is that the class name starts  
with a capital letter. So, Product is a class and product is the  
object. If you find that confusing, you could do things as you might in  
other languages:  
my\_product = Product.find(product\_id)  
but this looks ugly once you get used to the other notation 🙂

A local variable without the @ is simply that - a variable local to the  
specific method in your controller. With the ‘@’, it is an instance  
variable. Without going into the meanings of instance variables, the  
“Rails” meaning (not the Ruby one) for the variable is that it can be  
seen by code in both the controller and the view.

So, a temporary variable in a method in your controller would be without  
a ‘@’ - any variable which you would like to display in the view must be  
an instance variable.

If we look at the code in the original post,

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
@cart.add\_product(product)  
redirect\_to(:action =\> ‘display\_cart’)  
end

we can see that the details of the product that you find do not need to  
be displayed in the view. But, the cart details must be shown in that  
view. That’s why product (without @) and @cart (with @).

Lastly, params (though you didn’t ask) is a hash that communicates  
parameters from to an action. Think of it as input parameters to the  
controller’s method.

In summary, the controller method picks up the inputs from the params  
hash, processes it in local variables (without @) and writes its output  
to instance variables (with @). The view that is rendered as a result  
of the action reads values from the instance variables of the controller  
method and uses those values for rendering the output. When the user  
interacts with the view and clicks on something, it will send a request  
to a controller armed with relevant parameters (like the values of  
fields entered in a form) stored in the params hash - Full Circle! 🙂

Hope this helps.  
Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![running\_idiot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/898d66/32.png) [@running\_idiot](https://www.ruby-forum.com/u/running_idiot)\
**Post date:** [March 28, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/total-beginner-asks-a-few-questions/90355/6 "2007-03-28T15:11:52Z")

</div>

Ok, great post. The only other question I have, so I go to my Product  
class and find is inside a method. But I don’t think it’s refering to  
that. It’s a universal method that rails auto-creates?

---

<div class="post-metadata">

**Author:** ![running\_idiot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/898d66/32.png) [@running\_idiot](https://www.ruby-forum.com/u/running_idiot)\
**Post date:** [March 28, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/total-beginner-asks-a-few-questions/90355/7 "2007-03-28T17:28:21Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Ok, great post. The only other question I have, so I go to my Product  
> class and find is inside a method. But I don’t think it’s refering to  
> that. It’s a universal method that rails auto-creates?

Thanks. Find is a part of the ActiveRecord class. Note that your  
Product class inherits from the ActiveRecord (remember the first line of  
the model file was:  
class Product \< ActiveRecord::Base

ActiveRecord does the Object-Relational mapping (ORM) from a database  
row to the object. Check out:  
[Peak Obsession](http://ar.rubyonrails.com/classes/ActiveRecord/Base.html#M000344) (not  
just the anchor, the whole page)  
for much more information about ActiveRecord.

Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![running\_idiot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/898d66/32.png) [@running\_idiot](https://www.ruby-forum.com/u/running_idiot)\
**Post date:** [March 29, 2007, 11:52am UTC](https://www.ruby-forum.com/t/total-beginner-asks-a-few-questions/90355/8 "2007-03-29T11:52:52Z")

</div>

Ok so…  
in your view on your add to cart button you have this code :action  
=\> :add\_to\_cart, :id =\> product, this calls the add\_to\_cart method and  
the id it gives is the products id.  
Now in the add\_to\_cart  
Product.find(params[:id]),  
first the params[:id] gets the id of the product you submited because  
of the code on your add to cart button in view. so if you submited  
product with id=1 your Product.find will be Product.find(1) and so on,  
this params[:id] is to see what id to put in . If you still dont  
understand ill try to explain it more.  
So this code searches for product with id you submited.

Then you have @cart = find\_cart  
this makes a variable with your session because in your find\_cart  
method you created a new session that stores your products in cart

Then @cart.add\_product(product) this calls the add\_to\_product method  
in cart.rb.

Btw. I had the same problems when i was reading that part…Btw. youll  
probably have problems with cartitem later to figure out how thigs  
work…

I hope i helped a little.

P.S Sorry for my bad english…
