在DataMapper中自动获取列名

时间:2012-08-01 23:46:28

标签: sql schema datamapper database-schema

在ActiveRecord中,我有:

class Patient < ActiveRecord::Base
end

在DataMapper中我有:

class Patient
  include DataMapper::Resource

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

如何在DataMapper中自动获取列名?

2 个答案:

答案 0 :(得分:1)

你没有。 Datamapper的一个主要功能是在模型中定义映射,这就是为什么Datamapper在使用旧模式时是一个很好的选择,因此您可以使用rails列命名约定而无需更改整个数据库模式。

property :deleted,  Boolean,  field: 'isdeleted'  

查看文档以获取更多信息,但简而言之,这是datamapper的一项功能,而不是限制。我有点像在模型中明确定义表属性。

http://datamapper.org/why.html

答案 1 :(得分:1)

这个问题似乎有点旧,但有一个名为'dm-is-reflective'的宝石。它基本上为您完成了大部分映射。如果您正在使用时间戳,那么您需要使用以下内容映射DateTime列。 property :created_at, DateTime, :field => 'create_date'

只需安装gem,在require 'dm-is-reflective'的模型中使用它,然后在每个类中,您可以执行以下操作:

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

还有其他几种方法可以设置您的模型,但这种方式对我来说很合适。

了解更多信息...... https://github.com/godfat/dm-is-reflective

相关问题