I am considering using MongoDB (mongo-mapper) for a portion of my rails application. I am not ready to go whole hog MongoDB because there are too many useful gems that depend on a traditional DB.
That being said there are parts of my application that would be great to leverage a document database.
Has anyone had success mixing the two approaches? How do you link activerecord models with mongomapper models?
MongoMapper doesn't implement ActiveModel yet, but I think there are a few forks on github that do. You could use Mongoid instead (which does) and your relationships between Mongoid docs and ActiveRecord entries would just magically work. I know a number of people are doing that.
That said, I wouldn't want to mix them unless I absolutely had to have an RDBMS for some reason.
Here a presentation about this issue: http://nosql.mypopescu.com/post/541657350/presentation-blending-nosql-and-sql-at-confoo
I don't know ROR so I can't judge it is a good presentation.
http://railscasts.com/episodes/194-mongodb-and-mongomapper
http://www.mongodb.org/display/DOCS/Object+Mappers+for+Ruby+and+MongoDB
http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails
http://www.mongodb.org/display/DOCS/Ruby+Language+Center
You need to mixin mongomapper with the model class
This gives you freedom to define the key-value pairs other than activerecord
include MongoMapper::Document
Dead simple I think.
Related
I have a set of models I need to share among several rails3/sinatra/etc applications. I haven't seen anything like this yet and I'm curious what is the most effective way to go about this in a DRY manner? Can I specify a central area for models, can I create a gem that only brings in the models I need? How have other people approached this issue. I was thinking of making a smallish gem or something I don't yet know about to handle this.
I'm using Datamapper, but this is also a more general issue of structuring multiple rails/non rails ruby apps.
You might find it's easier to have a submodule in your repository that contains your models and associated unit tests than to create a full-fledged gem. This way you can patch from one app to the other instead of having to drive everything top down.
You can create public API to models you want to use in other apps.
In rails you should already have REST controllers, so you can use ActiveResource in the other apps.
You can pack your ActiveResource classes in a gem just to simplify thing and to keep things DRY.
You can check: http://api.rubyonrails.org/classes/ActiveResource/Base.html for some examples.
And it doesn't matter if you're using ActiveRecord or DataMapper.
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.
We are using a dynamic attributes plugin similar to this:
http://codaset.com/joelmoss/dynamic-attributes
Which allows us to store dynamic attributes in our rails model. Those dynamic attributes are in a single database column. We are facing performance issues because of this and I am wondering if MongoId, MongoMapper or other rails plugins will allow us to keep some attribute in ActiveRecord (keeping the < ActiveRecord::Base) but store the dynamic attributes in mongodb. We want to do this because we need to keep using MySQL for most of our existing system, but use MongoDB to store dynamic attributes about some models.
Basically this is what I am talking about:
http://www.railsinside.com/plugins/242-quickly-add-couchdb-to-existing-rails-models-with-stuffing.html
This plugin does the exact same thing using CouchDB.
I don't see any reason that MongoDB can't handle the same thing.
MongoMapper should implement the ActiveRecord pattern. However, Mongo also has some simple drivers for Ruby. All in all, the concept of storing dynamic attributes is a perfect use-case for MongoDB.
If you don't mind skipping "ActiveRecord" you can probably cook up your own using the basic Mongo drivers with very little work.
If you can't figure out the steps to do this, I would suggest pinging the groups (http://groups.google.com/mongodb-user/) and asking Kyle Banker directly (he's their ruby expert).
I'm looking for any pointers on how to write a rails web app without ActiveRecord.
A doc or an example of a (not too complex) web app using storage backends other than a relational database would be greatly appreciated.
It's not clear on what should be implemented in the model classes in order to make the rails app work without the ActiveRecord layer.
Thanks,
Of course it's possible, here, for example, MongoMapper is used instead of ActiveRecord:
http://railstips.org/blog/archives/2009/07/23/getting-started-with-mongomapper-and-rails/
Note that this will seem a lot easier with Rails 3. Rails team spent a lot of efforts on ORM agnosticism when pushing to beta. They've created a public API (ActiveModel) for different ORMs to implement, so that an ORM can serve as a drop-in replacement for ActiveRecord. That way you'll just be able to define models in terms of your ORM without any extra efforts.
DataMapper already has an implementation of ActiveModel in dm-rails, and there'll be more to come.
See this post by Yehuda Katz for details.
Is there a pattern or a gem (Ruby on rails) for modeling the wiki-like behaviour of a StackOverflow like web site?
I'm working on Ruby-on-Rails project so a gem or something in RoR would be appreciated but a well defined pattern is just as good.
Thanks
Did you try act_as_versioned? It is available as a gem, and adds simple versioning to any ActiveRecord model.
If you need more features, act_as_revisable might be interesting. According to the link, it adds the following features on top of act_as_versioned:
Pervasive Callbacks
Branching and Changesets
Deletes can be stored as a revision
Explicit is better than implicit
All data for a model is stored in one table
Wrapping up, requirements and installing