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
--
Posted via http://www.ruby-forum.com/.
-- 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 -- Posted via http://www.ruby-forum.com/.
Using <%= i.currentRole %> doesn''t work? Bob Silva http://www.railtie.net/> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > bounces@lists.rubyonrails.org] On Behalf Of Ed Schechter > Sent: Saturday, January 28, 2006 7:21 PM > To: rails@lists.rubyonrails.org > Subject: [Rails] Re: tyro Ruby questin > > -- am a step further along. > > <table> > <% for i in @list %> > <tr> > <td><%= i.instance_variable_get(:@attributes)%></td>> </tr> > <% end %> > </table> > > returns > > nameSpaghettisunToSatRolescurrentRoleQgroup_idid3loggedIn1baseRoleOmonDtlD > ef_id > namenamesunToSatRolescurrentRolegroup_idid2loggedInbaseRolemonDtlDef_id > nameESCHECHTZsunToSatRolescurrentRolePgroup_idid1loggedIn0baseRoleSmonDtlD > ef_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 > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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 -- Posted via http://www.ruby-forum.com/.
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:
> -- am a step further along.
>
> <table>
> <% for i in @list %>
> <tr>
> <td><%= i.instance_variable_get(:@attributes) %></td>
> </tr>
> <% end %>
> </table>
>
> returns
>
>
nameSpaghettisunToSatRolescurrentRoleQgroup_idid3loggedIn1baseRoleOmonDtlDef_i>
d> namenamesunToSatRolescurrentRolegroup_idid2loggedInbaseRolemonDtlDef_id
>
nameESCHECHTZsunToSatRolescurrentRolePgroup_idid1loggedIn0baseRoleSmonDtlDef_i>
d>
> 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
>
>
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/
> -----Original Message-----
> From: rails-bounces@lists.rubyonrails.org [mailto:rails-
> bounces@lists.rubyonrails.org] On Behalf Of Ed Schechter
> Sent: Saturday, January 28, 2006 8:42 PM
> To: rails@lists.rubyonrails.org
> Subject: [Rails] RE: Re: tyro Ruby question
>
> 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
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails