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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Why not just customize your code instead of relying on the scaffolding? RSL On 12/26/06, matt <matt-SaPSgiKzPPtdsTMtEp03Dg@public.gmane.org> wrote:> > > > 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 > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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:> > > 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 > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-26 14:16 UTC
Re: Excluding column names from a listing
Hi -- On Tue, 26 Dec 2006, matt wrote:> > > 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 -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Tue, 2006-12-26 at 09:16 -0500, dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote:> Hi -- > > On Tue, 26 Dec 2006, matt wrote: > > > > > > > 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 >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---