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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Dec 17, 8:17 pm, Rilindo Foster <rili...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Terrific, guys. Thanks! On Fri, Dec 18, 2009 at 11:27 AM, mat <mduafala-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On Dec 17, 8:17 pm, Rilindo Foster <rili...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@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: rilindo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org Web Site: http://www.monzell.com Primary: rilindo-BUHhN+a2lJ4@public.gmane.org Secondary: rilindo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org "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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.