1

在 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 中自动获取列名?

4

2 回答 2

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

于 2013-05-13T13:30:06.007 回答
1

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

property :deleted,  Boolean,  field: 'isdeleted'  

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

http://datamapper.org/why.html

于 2013-03-22T01:07:58.630 回答