Reputation: 495
In Rails I would simply use:
MyModel.column_names
to access the tables column names, but I am enduring my first fumblings with Sinatra (in order to better learn Ruby) and need to get the column names of a table into an array.
Previously, I have iterated over the params hash to extract key, value pairs:
params.each do |key, value|
#manipulate keys and values
end
and know that using:
@mm = MyModel.new
@mm.each do ....
leads to an undefined method 'each' error.
logger.info @mm.inspect
logs something like:
#<MyModel @id=nil @start=nil @end=nil @yada=nil @created_at=nil @updated_at=nil @foreign_id=nil>
So, how do I get those blasted column names I can see so clearly into an array?
Thanks...
UPDATE
Thanks to Doon for pointing me in the right direction with Datamapper properties.
I ended up making a private method column_names:
def self.column_names
self.properties.collect {|column| column.name.to_s }
end
which works a treat
Upvotes: 1
Views: 520
Reputation: 20232
Are you looking for properties
?
http://rubydoc.info/gems/dm-core/1.1.0/DataMapper/Model/Property
Upvotes: 2
Reputation: 2632
For any Ruby object you have the methods instance_variables
that returns the array of that object’s attributes. Is that too generic for you? I’m not familiar with DataMapper, there may be a specific method to do the equivalent of column_names
in ActiveRecord.
Upvotes: 0