• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Getting columns from model

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am writing a RoR application that connects to a couchDB database using couchrest_model.

The definition of my model is the following one:


class BdsDataAuthor < CouchRest::Model::Base
property :id, Integer
property :first_name, String
property :last_name, String
end



I would like to be able to get the list of the model columns, e.g., the result of
BdsDataAuthor.columns would be [id, first_name, last_name]

Is it possible? I saw that active_record provides this kind of method (column_names), but since I am not using active_record...

A solution that I tried is to create a method in the model class (it works but its not the "best" solution...):


def getColumns
columns = Array.new
columns << "id"
columns << "first_name"
columns << "last_name"
end



Any idea/suggestion to solve this problem?

Thank you
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suspect you could simply call .attributes on your object to achieve what you need.

Worst case scenario, fire up an irb console and do this to see what methods are available to you:
(YOUR_OBJECT.methods - Object.new.methods).sort

Better yet, add this code to your .irbrc file:


Now you can just open an irb console and simply type the following to get it:
YOUR_OBJECT.m

This saves me lots of time very often!
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic