A Describe Method / Pull list of Fields

3 views
Skip to first unread message

Rilindo Foster

unread,
Dec 17, 2009, 11:17:06 PM12/17/09
to rubyonra...@googlegroups.com
Is there a method that can either run a describe on the table or pull a list of fields from a table? The rationale is that I am looking for a while to automatically populate a drop down list so that I can execute a search based on the field name submitted from the form. I can, of course, populate it manually, but with 8+ models, it gets old fast.

Is this the way to do it, through? Or should I try something like Sphinx for these type of searches (which I am thinking of using anyway).

- Rilindo

Kip Cole

unread,
Dec 18, 2009, 3:07:51 AM12/18/09
to rubyonra...@googlegroups.com
Rilindo, the class method "columns" called on an ActiveRecord class is what you want.  Let's say you have a table called "books" with corresponding model Book.  Then:

Book.columns will return an array of column objects of the columns in the table.  Not so useful for a select list. 

Book.columns.collect{|x| x.name} will return an array of just the column names.  But this will contain the "id" column and maybe other foreign keys you don't want in a select list.  Therefore:

Book.columns.collect{|x| x.name}.reject{|x| x == 'id' || x.match(/_id\Z/)} might be closer to what you want,

Cheers, --Kip



Emery Finkelstein

unread,
Dec 18, 2009, 5:50:38 AM12/18/09
to Ruby on Rails: Talk
There is also the ActiveRecord::Base method column_names.


Which essentially returns the same thing as Book.column_names.collect
(&:name)

On Dec 18, 3:07 am, Kip Cole <kipco...@gmail.com> wrote:
> Rilindo, the class method "columns" called on an ActiveRecord class is what you want.  Let's say you have a table called "books" with corresponding model Book.  Then:
>
> Book.columns will return an array of column objects of the columns in the table.  Not so useful for a select list.
>
> Book.columns.collect{|x| x.name} will return an array of just the column names.  But this will contain the "id" column and maybe other foreign keys you don't want in a select list.  Therefore:
>
> Book.columns.collect{|x| x.name}.reject{|x| x == 'id' || x.match(/_id\Z/)} might be closer to what you want,
>
> Cheers, --Kip
>
>  Topic: A Describe Method / Pull list of Fields

> Rilindo Foster <rili...@gmail.com> Dec 17 11:17PM -0500

mat

unread,
Dec 18, 2009, 11:27:18 AM12/18/09
to Ruby on Rails: Talk
There are a number of activerecord methods that will probably get you
what you want. Take a look at columns or inspect.

http://api.rubyonrails.org/classes/ActiveRecord/Base.html

M

rilindo foster

unread,
Dec 18, 2009, 3:33:14 PM12/18/09
to rubyonra...@googlegroups.com
Terrific, guys. Thanks!

> --
>
> 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 rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
>

--
Rilindo Foster
AOL Instant Messenger: rilindo
Google Talk: ril...@gmail.com
Web Site: http://www.monzell.com
Primary: ril...@me.com
Secondary: ril...@gmail.com
"Rich bachelors should be heavily taxed. It is not fair that some men
should be happier than others. -- Oscar Wilde

Reply all
Reply to author
Forward
0 new messages