在 Django 中,您可以在 models.py 中完整描述您的模型。在带有 ActiveRecord 的 Rails 中,您在/models 目录中描述模型的一部分,在迁移中描述模型的一部分。然后 ActiveRecord 从现有的数据库表中检查模型属性。
但我发现迁移、列和表令人头疼。
我怎样才能像 Django 一样——只声明所有模型属性而不是从数据库表中内省(introspection)它们?
为了加分,请解释在何处以及为什么这不是一个好主意。 :)
最佳答案
如果您讨厌迁移,请尝试使用 NoSQL。没有迁移!
因此,您只需在需要时将属性添加到文档中即可。在您的代码中,处理它们可能不存在的事实,然后砰!
我从博客 about tekpub 中获取了以下模型定义(注意您没有继承 activerecord 的形式)同时推荐Herding Code podcast
class Production
include MongoMapper::Document
key :title, String, :required => true
key :slug, String, :unique => true, :required => true, :index => true
key :description, String
key :notes, String
key :price, BigDecimal, :numeric => true
key :released_at, Date, :default => Date.today
key :default_height, String, :default => '600'
key :default_width, String, :default => '1000'
key :quotes, String
#royalty info
key :producers, String
timestamps!
end
关于ruby-on-rails - 不设置数据库表的ActiveRecord? (像 Django 一样的声明式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2978046/