# Excluding column names from a listing

**URL:** <https://www.ruby-forum.com/t/excluding-column-names-from-a-listing/80423>\
**Category:** Rails\
**Created:** [December 26, 2006, 11:07am UTC](https://www.ruby-forum.com/t/excluding-column-names-from-a-listing/80423 "2006-12-26T11:07:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [December 26, 2006, 11:07am UTC](https://www.ruby-forum.com/t/excluding-column-names-from-a-listing/80423/1 "2006-12-26T11:07:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [December 26, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/excluding-column-names-from-a-listing/80423/2 "2006-12-26T13:31:24Z")

</div>

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

RSL

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [December 26, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/excluding-column-names-from-a-listing/80423/3 "2006-12-26T15:41:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [December 26, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/excluding-column-names-from-a-listing/80423/4 "2006-12-26T17:19:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [December 26, 2006, 3:17pm UTC](https://www.ruby-forum.com/t/excluding-column-names-from-a-listing/80423/5 "2006-12-26T15:17:30Z")

</div>

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](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](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](http://www.rubypal.com))
