Forum: Ruby on Rails Get column value of col1 .. col10

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Arnold (Guest)
on 2007-01-06 11:18
Hi,

  I have a table with column name like col1, col2, col3, col4 ... col10.
In rails, after I get the instance of the class, I would like to use a
for loop to retrieve values from these columns and put them into a hash.
The hash will be used for another find call. Is that possible?

  Thanks.

Arnold
Wes G. (Guest)
on 2007-01-06 19:36
Arnold wrote:
> Hi,
>
>   I have a table with column name like col1, col2, col3, col4 ... col10.
> In rails, after I get the instance of the class, I would like to use a
> for loop to retrieve values from these columns and put them into a hash.
> The hash will be used for another find call. Is that possible?
>
>   Thanks.
>
> Arnold

So do you want to basically copy just the part of the hash represented
by col1...col10?

Here's a start (not tested)

new_hash = {};
(1..10).each do |x|
  colname = "col#{x}"
  new_hash[colname] = @orig_AR_object.send(colname)
end

Wes
This topic is locked and can not be replied to.