List Info

Thread: question about hashes in views that model a table?




question about hashes in views that model a table?
user name
2006-08-15 11:27:34
if i have a table called cars with columns called year and
type, and i 
create a model and do this

mycars = Cars.find(:all)

in my view i have something like this

for cars in mycars
 do something like print out cars.type


i know that mycars in a hash where each key is a column name
paired 
with its corresponding value right?

without the for loop, how can i access the 2nd record
directly?
   mycars[0].type = ?



how does the variables in the view know that cars relate to
the Cars 
table?

is that for loop the same as something like this in c++

for (int i = 0; i < mycars.total; i++)









-- 
Posted via http://www.ruby-forum.com
/.
_______________________________________________
Rails mailing list
Railslists.rubyonrails.org
h
ttp://lists.rubyonrails.org/mailman/listinfo/rails
question about hashes in views that model a table?
user name
2006-08-15 12:44:23
ive also wondered this.

-- 
Posted via http://www.ruby-forum.com
/.
_______________________________________________
Rails mailing list
Railslists.rubyonrails.org
h
ttp://lists.rubyonrails.org/mailman/listinfo/rails
question about hashes in views that model a table?
user name
2006-08-15 15:17:52
baker1 wrote:
> if i have a table called cars with columns called year
and type, and i 
> create a model and do this
> 
> mycars = Cars.find(:all)
> 
> in my view i have something like this
> 
> for cars in mycars
>  do something like print out cars.type
> 
> i know that mycars in a hash where each key is a
column name paired 
> with its corresponding value right?

mycars will be an array of Car objects, each
containing an attribute
hash with column name keys.

> without the for loop, how can i access the 2nd record
directly?
>    mycars[0].type = ?

mycars[1].type = ?, though you probably will not know
what car
this is unless you use an :order option in your find that is
some sort of consecutive sequence number.

-- 
We develop, watch us RoR, in numbers too big to ignore.

_______________________________________________
Rails mailing list
Railslists.rubyonrails.org
h
ttp://lists.rubyonrails.org/mailman/listinfo/rails
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )