Tyro Ruby questin


#1

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

<% for i in @list %> <% end %>
<%= i.inspect %>

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

<%= i.inspect %>
to
<%= i.instance_variables %>

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


#2

– am a step further along.

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

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


#3

Using <%= i.currentRole %> doesn’t work?

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

<% for i in @list %>


<%= i.instance_variable_get(:@attributes)
%>

#4

Bob S. 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


#5

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| -%>

<%= list_item.name %> <% 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 :slight_smile:

On 1/28/06 7:21 PM, “Ed Schechter” removed_email_address@domain.invalid wrote:

returns

nameSpaghettisunToSatRolescurrentRoleQgroup_idid3loggedIn1baseRoleOmonDtlDef_i>
d

namenamesunToSatRolescurrentRolegroup_idid2loggedInbaseRolemonDtlDef_id

nameESCHECHTZsunToSatRolescurrentRolePgroup_idid1loggedIn0baseRoleSmonDtlDef_i>
d


#6

Have a look at content_columns attribute on your model.

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

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


#7

Bob, Steve

Thanks. That worked.

Ed