Excluding column names from a listing

I have a Listing model that I know I don’t want to display a few of the
columns, but I don’t want to hard-code the columns that I do want
either.

Is there a way to exclude certain columns?

This is what I have:
<% for column in Listing.content_columns %>

<%= column.human_name %>

This is roughly what I want (in midnight pseudo-code):
<% for column in Listing.content_columns[exclude: column_1, column_2] %>

<%= column.human_name %>

do-able?

Matt

Why not just customize your code instead of relying on the scaffolding?

RSL

Usually, it is better to make your own view instead of using the
scaffold, but sometimes it’s easier just to use the scaffolding.

What I did was override the content_columns method. Just take the
source:

def content_columns
@content_columns ||= columns.reject { |c| c.primary || c.name =~
/(_id|_count)$/ || c.name == inheritance_column }
end

And add the column names you want to exclude in the regex with _id and
_count.

Jason

Thanks,

That’s the hint I’ve needed. Hadn’t even thought about writing my own
method.

I’ve read in numerous books/articles not to put the code in the
view… and look what I was trying to do.

Thanks again.

Hi –

On Tue, 26 Dec 2006, matt wrote:

<%= column.human_name %>

This is roughly what I want (in midnight pseudo-code):
<% for column in Listing.content_columns[exclude: column_1, column_2] %>

<%= column.human_name %>

do-able?

Absolutely!

I’d say what’s missing is a content_columns_names method (at least I
can’t find one). It would look something like this:

class ActiveRecord::Base
def self.content_column_human_names
content_columns.map {|c| c.human_name }
end
end

You could put that into a plugin, or just roll it into your
application code (as below). In either case, it’s probably best to
put the array operation in the controller, which is a better place for
dynamic determination of names you don’t want.

So the whole thing might look like this (untested, and partially
pseudo-code):

In the controller:

def my_action
excludes = figure_out_which_names_you_don’t_want
all_names = Listing.content_columns.map {|c| c.human_name }
@header_names = all_names - excludes
end

And in the view:

<% @header_names.each do |name| %>
<%= name %>
<% end %>

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (Ruby for Rails)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)