# Tyro Ruby questin

**URL:** <https://www.ruby-forum.com/t/tyro-ruby-questin/98365>\
**Category:** Rails\
**Created:** [January 29, 2006, 2:39am UTC](https://www.ruby-forum.com/t/tyro-ruby-questin/98365 "2006-01-29T02:39:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ed\_Schechter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7ea924/32.png) [@Ed\_Schechter](https://www.ruby-forum.com/u/Ed_Schechter)\
**Post date:** [January 29, 2006, 2:39am UTC](https://www.ruby-forum.com/t/tyro-ruby-questin/98365/1 "2006-01-29T02:39:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ed\_Schechter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7ea924/32.png) [@Ed\_Schechter](https://www.ruby-forum.com/u/Ed_Schechter)\
**Post date:** [January 29, 2006, 4:21am UTC](https://www.ruby-forum.com/t/tyro-ruby-questin/98365/2 "2006-01-29T04:21:08Z")

</div>

– 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

---

<div class="post-metadata">

**Author:** ![Ed\_Schechter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7ea924/32.png) [@Ed\_Schechter](https://www.ruby-forum.com/u/Ed_Schechter)\
**Post date:** [January 29, 2006, 5:11am UTC](https://www.ruby-forum.com/t/tyro-ruby-questin/98365/3 "2006-01-29T05:11:19Z")

</div>

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

Bob S.  
[http://www.railtie.net/](http://www.railtie.net/)

> \<% for i in @list %\>  
>   
> \<%= i.instance\_variable\_get(:@attributes)  
> %\>

---

<div class="post-metadata">

**Author:** ![Ed\_Schechter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7ea924/32.png) [@Ed\_Schechter](https://www.ruby-forum.com/u/Ed_Schechter)\
**Post date:** [January 29, 2006, 5:41am UTC](https://www.ruby-forum.com/t/tyro-ruby-questin/98365/4 "2006-01-29T05:41:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ed\_Schechter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7ea924/32.png) [@Ed\_Schechter](https://www.ruby-forum.com/u/Ed_Schechter)\
**Post date:** [January 29, 2006, 5:56am UTC](https://www.ruby-forum.com/t/tyro-ruby-questin/98365/5 "2006-01-29T05:56:32Z")

</div>

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 🙂

On 1/28/06 7:21 PM, “Ed Schechter” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> returns

nameSpaghettisunToSatRolescurrentRoleQgroup\_idid3loggedIn1baseRoleOmonDtlDef\_i\>  
d

> namenamesunToSatRolescurrentRolegroup\_idid2loggedInbaseRolemonDtlDef\_id

nameESCHECHTZsunToSatRolescurrentRolePgroup\_idid1loggedIn0baseRoleSmonDtlDef\_i\>  
d

---

<div class="post-metadata">

**Author:** ![Ed\_Schechter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7ea924/32.png) [@Ed\_Schechter](https://www.ruby-forum.com/u/Ed_Schechter)\
**Post date:** [January 29, 2006, 6:02am UTC](https://www.ruby-forum.com/t/tyro-ruby-questin/98365/6 "2006-01-29T06:02:35Z")

</div>

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/](http://www.railtie.net/)

---

<div class="post-metadata">

**Author:** ![Ed\_Schechter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7ea924/32.png) [@Ed\_Schechter](https://www.ruby-forum.com/u/Ed_Schechter)\
**Post date:** [January 29, 2006, 6:28am UTC](https://www.ruby-forum.com/t/tyro-ruby-questin/98365/7 "2006-01-29T06:28:04Z")

</div>

Bob, Steve

Thanks. That worked.

Ed
