Only displaying selected columns from table


#1

My table has a number of columns that are for record keeping, and I
don’t want to display these column names back to the user when I display
data. Is there a way to tell RoR to only display column names that I
tell it to display? I’ve looked at the API, but the closest thing I saw
was content_columns, which doesn’t filter out all the columns I don’t
want to display. Is there a way to tell RoR to only list selected
columns?


#2

then siimply don’t show them. in your view if you don’t want to
display, for example, the description field, then don’t put <%=
thing.description -%> in your view.


#3

I understand I could do that, but is there a way to do this already
pre-built into RoR? This is a fairly common thing, and RoR should have
this kind of capability. I was hoping there was a place where I could
tell it to only display columns that I specifically tell him to, without
having to hardcode each column name into my view. That’s not very
dynamic, and kind of a drudge to do. Thanks for you advice though,
Chris.


#4

Daniel L. wrote:

I understand I could do that, but is there a way to do this already
pre-built into RoR? This is a fairly common thing, and RoR should have
this kind of capability. I was hoping there was a place where I could
tell it to only display columns that I specifically tell him to, without
having to hardcode each column name into my view. That’s not very
dynamic, and kind of a drudge to do. Thanks for you advice though,
Chris.

Oh, you clearly don’t have to hardcode each column name into your view.

If you are actually going to render all columns exactly the same way,
then just loop through a list of columns that you pass to your view.

Guest


#5

Guest wrote:

Daniel L. wrote:

I understand I could do that, but is there a way to do this already
pre-built into RoR? This is a fairly common thing, and RoR should have
this kind of capability. I was hoping there was a place where I could
tell it to only display columns that I specifically tell him to, without
having to hardcode each column name into my view. That’s not very
dynamic, and kind of a drudge to do. Thanks for you advice though,
Chris.

Oh, you clearly don’t have to hardcode each column name into your view.

If you are actually going to render all columns exactly the same way,
then just loop through a list of columns that you pass to your view.

Guest

I know I can loop through them, but that’s the problem; there are
certain fields I don’t want to display. If my table has these columns:
name, email, created_at, and created_by, all 4 column names are
displayed when I loop through the columns to display them. I only want
to display the name and email columns, while excluding the created_at
and created_by columns. Is there any way to do this or will I have to
write my own?


#6

Daniel L. wrote:

Chris.
certain fields I don’t want to display. If my table has these columns:
name, email, created_at, and created_by, all 4 column names are
displayed when I loop through the columns to display them. I only want
to display the name and email columns, while excluding the created_at
and created_by columns. Is there any way to do this or will I have to
write my own?

Daniel,

to avoid having to do that have a look at one of the automagic
scaffolding generators/plugins:
auto-admin/activescaffold/streamlined/toffee/hobo

HTH

Mike


Mike G. email : removed_email_address@domain.invalid
Technical Director fax : +353 68 470 01
XL CRS mobile: +353 87 677 2055
Lisselton phone : +353 68 470 10
Listowel skype : kingdomdude
Co. Kerry web : www.xlcrs.com


#7

Daniel,

I think you need to tell us how you are displaying your data. Rails
by itself does not do any display by default unless you use
scaffolding. if you are using scaffolding, it’s meant to be built
upon (hence the name ‘scaffolding’) and customized to your needs.

Chris


#8

Chris H. wrote:

Daniel,

I think you need to tell us how you are displaying your data. Rails
by itself does not do any display by default unless you use
scaffolding. if you are using scaffolding, it’s meant to be built
upon (hence the name ‘scaffolding’) and customized to your needs.

Chris

Chris H. wrote:

Daniel,

I think you need to tell us how you are displaying your data. Rails
by itself does not do any display by default unless you use
scaffolding. if you are using scaffolding, it’s meant to be built
upon (hence the name ‘scaffolding’) and customized to your needs.

Chris

I have a “children” table that is assocated by “parent_id” to a
“parents” table. I want to display the children, but I only want to
display relevant information back to the user about the child. For
instance, in my “children” table I have columns such as created_at and
created_by that are only for record keeping, and don’t need to be
displayed back to the user. I’m trying to find a simple way to only
display selected columns from the “children” table. One way I can do it
is by grabbing all the columns, and then only displaying the selected
ones in the view:

in controller

@children = Child.find_all_by_parent_id(@parent.id)

In my view, I want to display the column names from left to right, and
then the information for each child underneath:

in my list view

<% for column in Child.content_columns %>
<% if column.name == ‘first_name’ || column.name == ‘last_name’
||column.name == ‘gender’ %>

<%= column.human_name %>
<% end %>
<% end %>

<% for child in @children %>
<% for column in Child.content_columns %>
<% if column.name == ‘first_name’ || column.name == ‘last_name’
||column.name == ‘gender’ %>

<%=h child.send(column.name) %>
<% end %>
<% end %>
<% end %>

This way will work, and only display the first_name, last_name, and
gender column names and then assocated information of the child.
However, this is rather complex when I want to display more information,
and not very dynamic. Another way I’m looking into is only returning
the selected columns that I want (some of the syntax might be off; I’m
still trying to figure this part out):

in controller

@children = Child.find_by_parent_id(@parent.id, :select
=> ‘first_name’, ‘last_name’, ‘gender’)

The syntax isn’t correct I don’t think, but the idea is there. I only
want to select those 3 columns for each child who matches the parent’s
id. Then, I want to display the column name and child information
again:

in my list view

<% for column in @children %> # lines where I’m having trouble

<%= column.human_name %> <% end %>

list child information below column names

<% for child in @children %>
<% for column in child.columns %>

<%=h child.send(column.name) %>
<% end %>
<% end %>

This is essentially where I’m at right now. The problem is, once I’ve
returned a list of the child objects to @children with only the selected
columns (first_name, last_name, gender), how do I access just those
columns and their names? I’m just looking for a simpler way to only
display selected columns without having to have a bunch of “if”
statements to check each column. Any help would be greatly appreciated.
-Daniel


#9

well, part of the problem is that you are using content_columns to
loop through your models attributes. i think you need to approach it
from a different angle. it seems like you have this idea that
everything is related somehow to column names and you need to break
out of that mode of thinking.

there really isn’t a need to use the content_columns approach, unless
you see your database schema being dynamic. just hardcode the column
names in the view and be done with it.

Your model is the representation of what is in the database. Don’t
think of it as columns in a record, think of it more as an object,
which is what it is once you’ve retrieved the information from the
database. once you do that you will see that it isn’t as difficult as
you are making it out to be.

for instance, what you will normally see is something like

in controller

@children = Child.find_all_by_parent_id(@parent.id)

in view

First Last Gender <% @children.each do |child| -%> <%= child.first_name -%> <%= child.last_name -%> <%= child.gender -%> <% end -%>

or better yet, use a partial

First Last Gender <%= render :partial => 'child', :collection => @children -%>

_child.rhtml (partial)

<%= child.first_name -%> <%= child.last_name -%> <%= child.gender -%>

#10

@children = Child.find_by_parent_id(@parent, :select => “first_name,
last_name, gender”)

That should do it.

Cam

On Jun 12, 1:52 pm, Daniel L. removed_email_address@domain.invalid