This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Ruby and the fly likes Getting columns from model Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Languages » Ruby
Bookmark "Getting columns from model" Watch "Getting columns from model" New topic
Author

Getting columns from model

Paulo Carvalho
Ranch Hand

Joined: Nov 12, 2008
Posts: 57
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
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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!


A good workman is known by his tools.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Getting columns from model