I’m working on my first rails project after having gone through quite a
bit of Ruby on Rails/Up and Running.
I have a model ‘Configuration’.
In the view that got scaffolded for me (I’m still back on rails version
1.2.3) I have a list.rhtml that looks like this
<% for column in Configuration.content_columns %>
<%= column.human_name %> |
<% end %>
<% for configuration in @configurations %>
<% for column in Configuration.content_columns %>
<%=h configuration.send(column.name) %> |
<% end %>
[ snip … ]
My Configuration table has many columns, and in the list view, I’d just
like to show the ‘important’ columns. Initially in my model I defined
class Configuration < ActiveRecord::Base
def self.important_columns
return [ :description, :rate, :duration, :host]
end
end
This didn’t work, but I realized I’m returning strings, not objects. I
modified the view to be
<% for column in Configuration.important_columns %>
<%=h configuration.send(column) %> |
<% end %>
But I now no longer have access to human_name for the titles. How can I
get access to the column objects, knowing their names?
I thought I could perhaps figure this out by myself if I could find the
source code for content_columns, but I couldn’t find it, any hints on
how I might have accomplished that?
TIA,
Leonard
I’m working on my first rails project after having gone through quite a bit of Ruby on Rails/Up and Running.
…
But I now no longer have access to human_name for the titles. How can I get access to the column objects, knowing their names?
I thought I could perhaps figure this out by myself if I could find the
source code for content_columns, but I couldn’t find it, any hints on
how I might have accomplished that?
Look in the API docs for “content_columns”. Then click the “source”
link
to see the method definition. For 1.2.5 it’s this:
# File vendor/rails/activerecord/lib/active_record/base.rb, line
781
781: def content_columns
782: @content_columns ||= columns.reject { |c| c.primary ||
c.name =~ /(_id|_count)$/ || c.name == inheritance_column }
783: end