Only displaying selected columns from table

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.

Daniel Legrand wrote:

Guest wrote:   

Daniel Legrand 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?

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

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

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

That should do it.

Cam

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 <tr><th>First</th><th>Last</th><th>Gender</th></tr> <% @children.each do |child| -%>   <tr><td><%= child.first_name -%></td><td><%= child.last_name -%></td><td><%= child.gender -%></td> <% end -%>

or better yet, use a partial

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

_child.rhtml (partial)

<tr>   <td><%= child.first_name -%></td>   <td><%= child.last_name -%></td>   <td><%= child.gender -%></td> </tr>