mentby.com
Blog | Jobs | Help | Signup | Login

A Describe Method / Pull list of Fields



"Ëaz·š™ëa¡Ûajם譅êëºvzÇ+‰·¨žØ^µ¦åzŠéºYZ–+


Rilindo Foster Thu, 17 Dec 2009 20:17:40 -0800

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 <rilindo*******> Dec 17 11:17PM -0500

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

--

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


Kip Cole Fri, 18 Dec 2009 00:09:09 -0800

There is also the ActiveRecord::Base method column_names.

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

--

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


Emery Finkelstein Fri, 18 Dec 2009 02:51:14 -0800

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

--

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


mat Fri, 18 Dec 2009 12:09:56 -0800

Terrific, guys. Thanks!

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

--

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


Rilindo Foster Fri, 18 Dec 2009 12:33:59 -0800



Related Topics

Post a Comment