gpt4 book ai didi

ruby-on-rails - 不设置数据库表的ActiveRecord? (像 Django 一样的声明式)

转载 作者:搜寻专家 更新时间:2023-10-30 19:44:33 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com