开发者

Migrating an Activerecord database to Mongoid

开发者 https://www.devze.com 2023-02-21 20:25 出处:网络
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 Active

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.


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!


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.


...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.

0

精彩评论

暂无评论...
验证码 换一张
取 消