# Ruby on rails book, fundamental question

**URL:** <https://www.ruby-forum.com/t/ruby-on-rails-book-fundamental-question/57984>\
**Category:** Rails\
**Created:** [April 17, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-book-fundamental-question/57984 "2006-04-17T14:49:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [April 17, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-book-fundamental-question/57984/1 "2006-04-17T14:49:36Z")

</div>

Hi guys,

bought the ROR book. I understand most of it and really like it. There  
is just on thing I just dont get.  
Lets take this code for example.

def list  
@products = paginate :product, :per\_page =\> 10  
end

As far as I understand @products should be some kinda hash or array,  
with the data of the Database inside.  
How can I display this array ?

One solution is to do it like this (like in the book)

\<% for product in @products -%\>  
 ![]()  
\<% end %\>

But I want to understand (!) how this works. Something like puts  
@products[“title”] or puts @products[:title] which obviously doesnt  
work.

I think this would help to understand the whole concept.

Thanks alot

---

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [April 17, 2006, 2:58pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-book-fundamental-question/57984/2 "2006-04-17T14:58:36Z")

</div>

I will try to explain this. If you do the same command from  
./script/console “@products = Product.find(:all)”, you will be able to  
access this hash by calling each element as @products[1].title or by  
other  
keys in the hash. Does that make since?

John

---

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [April 17, 2006, 3:04pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-book-fundamental-question/57984/3 "2006-04-17T15:04:57Z")

</div>

Hi John,

I just found this out myself about 1 Minute ago.  
Totally makes sense. Makes things much clearer.

I have one more question though

In the rhtml file you find something like this  
\<%= link\_to ‘Show’, :action =\> ‘show’, :id =\> product %\>

in a controller you find:

def show  
@product = Product.find(@params[:id])  
end

Everything makes sense. Im just not shure where the @params comes from.  
Is this some part of the framework ?

Thanks alot again… 😉

John H. wrote:

> I will try to explain this. If you do the same command from  
> ./script/console “@products = Product.find(:all)”, you will be able to  
> access this hash by calling each element as @products[1].title or by  
> other  
> keys in the hash. Does that make since?
> 
> John

---

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [April 17, 2006, 3:20pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-book-fundamental-question/57984/4 "2006-04-17T15:20:10Z")

</div>

The @params would be the parameter’s that you pass with the :id =\>  
product  
or if you pass other params as well. On the other question the only way  
that I know how to print a whole array is with the @products.each or  
for  
product in @products. Hope that helps,

John

---

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [April 17, 2006, 3:06pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-book-fundamental-question/57984/5 "2006-04-17T15:06:56Z")

</div>

I forgot. Is there a command in ruby to output the whole array ?  
Just for me to check ?

---

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [April 17, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-book-fundamental-question/57984/6 "2006-04-17T15:44:52Z")

</div>

2006/4/17, Jan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I forgot. Is there a command in ruby to output the whole array ?  
> Just for me to check ?

Try my\_array.inspect, that should do what you’re after.

hth,  
Douglas

---

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [April 17, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-book-fundamental-question/57984/7 "2006-04-17T16:49:38Z")

</div>

Douglas,

Thanks for the inspect command. I had never seen that, it will be real  
helpful now.

---

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [April 17, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-book-fundamental-question/57984/8 "2006-04-17T16:37:13Z")

</div>

Also of interest might be

logger.info @product

which will write out the array to the relevant log file

or something like

p @product

will print out the array (regardless of if the content is a string or  
not) to the console running webrick

---

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [April 17, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-book-fundamental-question/57984/9 "2006-04-17T17:39:21Z")

</div>

@products is an array of Product objects. In your code you are iterating  
through the array and for each Product object you are accessing the  
attribute image\_url.

@products[“title”] would be trying to access an array with a string as  
the index, which doesn’t work (this would work with a hash though).

Jan wrote:

> As far as I understand @products should be some kinda hash or array,  
> @products[“title”] or puts @products[:title] which obviously doesnt  
> work.
> 
> I think this would help to understand the whole concept.
> 
> Thanks alot

–  
Sau S.

[http://blog.saush.com](http://blog.saush.com)  
[http://read.saush.com](http://read.saush.com)  
[http://jaccal.sourceforge.net](http://jaccal.sourceforge.net)
