3

最近我从 django 开发转变为全职 Rails 工作,这是一家相当小的商店,我一边走一边从书本上捡东西。

上周,当我了解到 Rails 的模型不反映数据库中的内容时,我的心智模型受到了重大打击。

请参阅差异示例:http ://www.peterkrantz.com/2009/rails-grails-django-models/

我很好奇的是,我如何不断修改模型以支持新的数据类型和关系?

另外,有没有办法在模型文件中显示特定类的表中的所有属性?

谢谢

4

2 回答 2

5

我认为迁移是您正在寻找的。

如果您想要模型文件中显示的所有列,请使用注释 gem

于 2011-12-20T16:38:41.443 回答
1

This depends on an ORM you use. While ActiveRecord indeed fetches schema from the database, Mongoid offers to annotate your models. Here's one of models from my current project:

class DailyStat

  include Mongoid::Document

  identity :type => String

  field :app_id, :type => Integer
  field :date, :type => DateTime

  field :stats, :type => Hash
  field :totals, :type => Hash
  field :counts, :type => Hash
end

This is so because of schemaless nature of MongoDB. Without such declarations, all fields would have dynamic type (String by default). And declarations help to enforce types.

Also, with MongoDB you have no migrations and annotate gem won't help here.

于 2011-12-20T16:39:15.667 回答