sites
sites

Reputation: 21795

Automatically get column names in DataMapper

In ActiveRecord I had:

class Patient < ActiveRecord::Base
end

In DataMapper I had:

class Patient
  include DataMapper::Resource

  property :id, Serial
  property :name, String
  has n, :orders
  # ... a lot of properties and associations more
end

How to automatically get column names in DataMapper?

Upvotes: 1

Views: 220

Answers (2)

colevoss
colevoss

Reputation: 46

This question seems to be a little old but there is a gem called 'dm-is-reflective'. It essentially does most of your mapping for you. If you are using timestamps then you will need to map your DateTime columns with something like the following. property :created_at, DateTime, :field => 'create_date'

Just install the gem, require it in your model with require 'dm-is-reflective' and then in each class you can do something like the following:

class Comment
include DataMapper::Resource
is :reflective

    # manually mapping a timestamps field
property :created_at, DateTime, :field => 'create_date' 
reflect /.*/  # This is just one way to do this. 
end

There are a couple of other ways to set up your models but this way worked just fine for me.

For more information... https://github.com/godfat/dm-is-reflective

Upvotes: 1

Norto23
Norto23

Reputation: 2269

You don't. One of the main features of Datamapper is that you define your mappings in your models, which is why Datamapper is a great choice when working with legacy schemas so you can use rails column naming conventions without having to change a whole database schema.

property :deleted,  Boolean,  field: 'isdeleted'  

Checkout the docs for more info but in short, this is a feature of datamapper, not a restriction. And I kind of like having the table properties defined explicitly in the model.

http://datamapper.org/why.html

Upvotes: 1

Related Questions