将 Activerecord 数据库迁移到 Mongoid

发布于 2024-10-30 02:24:06 字数 394 浏览 1 评论 0原文

我是 Rails 编程新手。我正在考虑按照 railscast 教程 实现 devise 和omniauth 身份验证。由于我还不了解 mongoid,所以我打算从 Activerecord 开始。我想最终我想使用 Mongoid。

我如何从 Activerecord 迁移到 Mongoid?

我只想开始我的项目。特别是当我的用户很少时,Activerecord 可能就足够了。我以前从未这样做过,所以希望有人可以告诉我这种方法是否会带来比其价值更多的麻烦。对我来说现在花更多时间学习 mongoid 更有意义吗?

我期待收到你们这些 Rails 老兵的来信。

I'm new to Rails programming. I was thinking about implementing devise and omniauth authentication per railscast tutorial. Since I don't know mongoid yet, I was planning on just starting with Activerecord. Eventually I want to use Mongoid I think.

How would I do a migration from Activerecord to Mongoid?

I just want to get rolling with my project. Especially when I have few users Activerecord will probably be sufficient. I've never done this before, so hopefully someone can tell me if this approach is going to be way more trouble than it's worth. Does it make more sense for me to take more time now to learn mongoid now instead?

I'm looking forward to hearing from you Rails veterans.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇妻 2024-11-06 02:24:06

您可以像使用 ActiveRecord 一样使用 mongoid 构建模型、控制器和视图,因此几乎没有什么区别。最大的区别在于数据的实际存储和检索方式,这会影响您的模型,从而直接影响您的代码。

像 mongoDB 这样的无模式数据库无法像 MySQL 那样保护您,并且没有简单的方法可以使用 Mongoid 进行迁移。

如果您刚开始,您可能应该使用 ActiveRecord,因为大量信息依赖于您将 ActiveRecord 与关系数据库一起使用。

然而,切换到 mongo/mongoid 绝对值得任何感知到的痛苦,但除非您使用过关系数据库和 ActiveRecord,否则您可能不会意识到 mongo/mongoid 有多棒!

You build your models, controllers and views exactly the using mongoid as you do using ActiveRecord, so there's little difference there. The big difference is in the way your data is actually stored and retrieved, which affects your models, which directly impacts your code.

A schema-less database like mongoDB can't protect you like MySQL can, and there is no simple way to do migrations using Mongoid.

If you're starting out, you should probably go with ActiveRecord just because the a lot of the information out there relies on you using ActiveRecord with a relational database.

However, a switch to mongo/mongoid is definitely worth any perceived pain, but unless you've used a relational database and ActiveRecord, you may not appreciate just how awesome mongo/mongoid can be!

停顿的约定 2024-11-06 02:24:06

我相信 ActiveRecord 就足够了。请考虑那些方便但无法与 Mongoid 一起使用的小 gem/插件。教程、截屏视频 - 绝大多数都是基于默认的 ORM/Mysql。

我认为目前还不值得花时间在 Mongoid 上。

I believe ActiveRecord is sufficient. And please think about those small gems/plugins which are handy but not able to work with Mongoid. Tutorials, screencasts - vast majority of them are based on default ORM/Mysql.

For now it's just not worth to spent time on Mongoid I think.

花伊自在美 2024-11-06 02:24:06

...并且没有简单的方法可以使用 Mongoid 进行迁移。

这不是真的。在 Mongoid 中创建迁移实际上非常简单。如果您想向数据库表添加一列,只需将其作为“字段”添加到 Model 类的顶部,如下所示:

class User
  include Mongoid::Document

  field :email, type: String
  field :phone, type: String
  field :reputation, type: Integer
end

不创建迁移,不整理数据库。只需根据需要添加/删除字段,重新启动服务器,就可以了。但是,您应该警惕删除字段,因为它们可能会破坏您引用它们的代码。

...and there is no simple way to do migrations using Mongoid.

This isn't true. It is actually quite simple to create migrations in Mongoid. If you want to add a column to a database table, simply add it as a "field" to the top of the Model class like so:

class User
  include Mongoid::Document

  field :email, type: String
  field :phone, type: String
  field :reputation, type: Integer
end

No creating migrations, no raking the database. Just add/remove fields as needed, restart the server, and you're good to go. You should however be wary of removing fields as they can break your code where you referenced them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文