Ruby on rails book, fundamental question


#1

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


#2

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


#3

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… :wink:

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


#4

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


#5

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


#6

2006/4/17, Jan 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


#7

Douglas,

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


#8

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


#9

@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://read.saush.com
http://jaccal.sourceforge.net