Forum: Ruby on Rails tyro Ruby questin

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.
F5c659335007b04061aa1b84e2992f31?d=identicon&s=25 Ed Schechter (Guest)
on 2006-01-29 02:39
Trying to print out a simple database grid, using, with column headers
such as 'SunToSatRoles', 'PrimaryRoles', etcetera.

Iterating through an object @List which is populated thusly

    @list = mymodel.find(:all, :order => mymodel.editlist_order)

When I do something like

<table>
	<% for i in @list %>
		<tr>
			<td><%= i.inspect %></td>
		</tr>
	<% end %>
</table>

I get

#"Spaghetti", "sunToSatRoles"=>nil, "currentRole"=>"Q", "group_id"=>nil,
"id"=>"3", "loggedIn"=>"1", "baseRole"=>"O", "monDtlDef_id"=>nil}>
#"name", "sunToSatRoles"=>nil, "currentRole"=>nil, "group_id"=>nil,
"id"=>"2", "loggedIn"=>nil, "baseRole"=>nil, "monDtlDef_id"=>nil}>
#"ESCHECHTZ", "sunToSatRoles"=>"", "currentRole"=>"P", "group_id"=>nil,
"id"=>"1", "loggedIn"=>"0", "baseRole"=>"S", "monDtlDef_id"=>nil}>
.

When I change
    <td><%= i.inspect %></td>
to
    <td><%= i.instance_variables %></td>

my output is

    @attributes
    @attributes
    @attributes

Further inspection shows @attributes is an array of length 1.  My
question is, given that the inspect method shows what I need, how do I
reference the list of columns?  (Once there, I can reference
i.currentRole to list values, etc.)

Appreciate any pointers --

Ed Schechter
F5c659335007b04061aa1b84e2992f31?d=identicon&s=25 Ed Schechter (Guest)
on 2006-01-29 04:21
-- am a step further along.

<table>
	<% for i in @list %>
		<tr>
			<td><%= i.instance_variable_get(:@attributes) %></td>
		</tr>
	<% end %>
</table>

returns

nameSpaghettisunToSatRolescurrentRoleQgroup_idid3loggedIn1baseRoleOmonDtlDef_id
namenamesunToSatRolescurrentRolegroup_idid2loggedInbaseRolemonDtlDef_id
nameESCHECHTZsunToSatRolescurrentRolePgroup_idid1loggedIn0baseRoleSmonDtlDef_id

This contains the database field names and values.  How can I pull the
field names from this (or am I going about it the wrong way?

Ed
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-01-29 05:11
(Received via mailing list)
Using <%= i.currentRole %> doesn't work?

Bob Silva
http://www.railtie.net/

> 	<% for i in @list %>
> 		<tr>
> 			<td><%= i.instance_variable_get(:@attributes)
%></td>
F5c659335007b04061aa1b84e2992f31?d=identicon&s=25 Ed Schechter (Guest)
on 2006-01-29 05:41
Bob Silva wrote:
> Using <%= i.currentRole %> doesn't work?
>

It does, but I don't want to hardcode.  i.currentRole, i.baseRole, ...

I want to create a generalized loop to cycle through the table's (and
hence, model's_ attributes.  It obviously can be done -- the i.inspect
method is doing it.

What am I missing?

Thanks
Ed
7e5be8d0d2537a6f6f00aada5541881e?d=identicon&s=25 Steve Ross (Guest)
on 2006-01-29 05:56
(Received via mailing list)
I may be totally misunderstanding your question but.

Try script/console. I believe you're looking for the class method
ActiveRecord#columns. Example:

List.columns.each{|c| puts c.name}  #assumes a model called "List"

That should give you all the column names. The corollary to this is that
to
make a name=>value hash, you can simply:

bar = List.find(:first)
nv_hash = {}
List.columns.each{|c| nv_hash[c.name] = bar.send(c.name)}

Moving to your rhtml example:

<% List.columns.each do |list_item| -%>
<tr><td><%= list_item.name %></td></tr>
<% end -%>

I don't have a list model or foo's and bar's in any of my projects, but
playing around in the console should give you a good deal of insight
into
what's going on there.

-- sent from my Mac PowerBook running OS X, if you've been tracking that
thread :)


On 1/28/06 7:21 PM, "Ed Schechter" <schechtere@hotmail.com> wrote:

> returns
>
>
nameSpaghettisunToSatRolescurrentRoleQgroup_idid3loggedIn1baseRoleOmonDtlDef_i>
d
> namenamesunToSatRolescurrentRolegroup_idid2loggedInbaseRolemonDtlDef_id
>
nameESCHECHTZsunToSatRolescurrentRolePgroup_idid1loggedIn0baseRoleSmonDtlDef_i>
d
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-01-29 06:02
(Received via mailing list)
Have a look at content_columns attribute on your model.

<table>
<% for i in @list %>
  <tr>
  <% for column in List.content_columns %>
     <td><%= h i.send(column.name) %></td>
  <% end %>
  </tr>
<% end %>
</table>



Bob Silva
http://www.railtie.net/
F5c659335007b04061aa1b84e2992f31?d=identicon&s=25 Ed Schechter (Guest)
on 2006-01-29 06:28
Bob, Steve

Thanks.  That worked.

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