从Django到Rails,您如何继续使用rails模型?

时间:2011-12-20 16:33:20

标签: ruby-on-rails django-models rails-migrations rails-models

最近我从django开发转变为全职轨道工作,这是一个相当小的商店,我正在从书本中挑选东西并随时随地拿起东西。

上周,当我了解到rails的模型没有镜像数据库中的内容时,我的心理模型受到重大打击。

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

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

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

由于

2 个答案:

答案 0 :(得分:5)

我认为migrations正是您所寻找的。

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

答案 1 :(得分:1)

这取决于您使用的ORM。虽然ActiveRecord确实从数据库中提取了模式,但Mongoid提供了对模型进行注释的功能。这是我当前项目中的一个模型:

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

这是因为MongoDB的无模式特性。如果没有这样的声明,所有字段都将具有动态类型(默认情况下为String)。声明有助于强制执行类型。

此外,使用MongoDB,您没有迁移,annotate gem也无法帮助。