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 %>     <th><%= column.human_name %></th>

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

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

matt wrote:

Hi --

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 %>   <th><%= column.human_name %></th>

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

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| %>      <th><%= name %></th>    <% end %>

David

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.