It's worth noting that this example only shows a normal table to table relationship. In "mongoDBland" this is called a *db reference*, there is, a reference between 2 collections.
There's this simple relationship between tables (actually collections in mongoDB), and there's the new thing which is embedded documents. To embeded the Task model inside a Project, in this example, you have to change the file "app/models/task":
# models/task.rb
class Task
include MongoMapper::EmbeddedDocument
...
That's it.
And to those asking "Should I embeded or reference?", read more this subject at:
Very nice Ryan!
I only looked into MongoDB itself for a few but never tried to do anything with it in Rails. That MongoMapper Gem seems to work very well! I will definitely give it a shot!
@Levy, great points. Here I wanted to show what Rails developers are most familiar with to get started. I hope to make a more advanced episode in the future where I show some of the things that make MongoDB more unique (most were touched on in that blog post).
@Mantas, that's a great question. AFAIK Authlogic still does not support MongoMapper however there is an open ticket on it.
Devise does work with Mongo, I've been playing with it this week.
What I would love is a railscast on gridfs. I'm fairly new to rails and I got Devise to work with mongo and mongomapper, but gridfs is confusing me no end.
Good to know about MongoDB. I have a question though. Where is the data stored in mongodb? Is there any way to perform full text search and db backups, etc like traditional relational databases would allow us to do?
Also how is it different from storing data in xml files?
Ryan, what's reason for usage of "h" escape_html method within Rails 2.3.5?
By the way, MongoDB’s performance as compared to others
http://prajwal-tuladhar.net.np/2009/11/15/500/mongodbs-performance-as-compared-to-others-esp-couchdb/
I just wanted to mention that there was also a screencast series recently done on MongoDB by Joon You (rubyhead) which can be found on http://teachmetocode.com.
Thanks a LOT for feeding us Awesome screencast! I can't wait for the next screencast that explains more advanced mongo mapper like callback, how to tackle many to many relation, and probably if there is any asynchronous way for mongodb using ruby.
Where is the clap of your finger?? I was expecting it !
Script to install MongoDB on OS X using Homebrew and set it to automatically run: http://gist.github.com/265272
One note about the screencast: Tasks should have been embedded in the Projects collections, because a single task isn't shared among projects. This is the true Mongo way. MongoMapper supports embedded models in a very nice object-oriented way.
I saw this used with Rackamole (http://rackamole.com/) Maybe you can look into Rackamole too! It's a great rack plugin for monitoring a rails application.
I'm not used to this kind of DBM, I mostly use SQL (MySQL and MSSQL) so I can't really see the big avantages of this kind of DBM...
I expected to hear some use cases for such databases. I don't suppose they perform as good as *SQL, and I guess they're only supposed to be used in certain situations? I found this was missing from the railscast.
Otherwise nice railscast.
Doesn't work for me. I can't create the scripts because of active record. How do I either turn off active record, or what do I put in databases.yml (currently it's blank) to make it stop wanting to connect to the db?
Ryan; Love your screencast on mongodb. I just wanted to add, for those users who use macports, there is a simpler way of installing mongodb and making sure it's up to date. Use the sudo port install mongodb command and you are all set to go with mongo!
Is it intentional (or my personal failing) that prevents me from being able to fast-forward and skip around your episodes when viewed in iTunes? The thumb is effectively disabled.
OK, it was of course my problem. The in-episode fast forward (at the bottom inside the frame) still does not work, but the iTunes scrubber (top, green window) does as always. Strange, but nevermind.
@Matthew: I have encountered the same problem. Running the console everything's fine but when trying to open the site in a browser passenger obviously tries to load ActiveRecord which then complains since no database is configured. After hours of trial and error I will give up on MongoMapper until it works fine.
I was working on a similar example using Rails3.Beta and MongoMapper. The biggest difficulty I had was that the models no longer inherited from ActiveRecord, and it felt like I had to change things significantly to make things work.
For example I couldn't use "form_for @object" because it would complain that @object didn't have method 'foo' (in this case I believe it was missing 'class_name'). I could get form_tag to work, but the experience made me wonder how well mongomapper+mongodb will work as a straight replacement to AR.
Well, I re-watched your cast and noticed that you didn't have these problems with 2.3.x!! Hopefully I am doing something wrong on my end, but am going to try with 2.3.x right now..
Very exciting though, I'm hoping to use Rails+mongo for my next side project, thank you for covering it here.
A little followup: I went back and tried this with Rails 2.3.5 and everything worked much better. I did have problems with the generator scripts; it seems like they aren't too tolerant of other ORM's. Once I got everything laid out though it worked brilliantly.
I did have problems with my database.yml file though; In your cast you don't empty it, but if I left database.yml populated with the sqlite information it would barf when I started the server. An empty database.yml made things much better.
How to migrate the _data_ with mongodb? Although I dont need to modify the schema anymore I have to adjust the data from time to time. Is there a mechanism to collect such "db-patches" which can modify existing data in a comprehensive manner in the different environments?
it's really useful
Thank you, Ryan!
Great screencast, Ryan!
It's worth noting that this example only shows a normal table to table relationship. In "mongoDBland" this is called a *db reference*, there is, a reference between 2 collections.
There's this simple relationship between tables (actually collections in mongoDB), and there's the new thing which is embedded documents. To embeded the Task model inside a Project, in this example, you have to change the file "app/models/task":
# models/task.rb
class Task
include MongoMapper::EmbeddedDocument
...
That's it.
And to those asking "Should I embeded or reference?", read more this subject at:
http://www.mongodb.org/display/DOCS/Schema+Design#SchemaDesign-Embedvs.Reference
Cheers!
Levy
Very nice Ryan!
I only looked into MongoDB itself for a few but never tried to do anything with it in Rails. That MongoMapper Gem seems to work very well! I will definitely give it a shot!
Thanks.
Was hoping for a railscast to help me get started with MongoDB. Thanks! :)
Thank you, great cast as always.
Any ideas how to handle authentication with mongomapper? Does authlogic work with it?
@Mantas:
Devise should work with it. I'm not sure about Authlogic although.
Just a quick note that there is a MongoDB hosting service called MongoHQ.
http://www.mongohq.com
@Levy, great points. Here I wanted to show what Rails developers are most familiar with to get started. I hope to make a more advanced episode in the future where I show some of the things that make MongoDB more unique (most were touched on in that blog post).
@Mantas, that's a great question. AFAIK Authlogic still does not support MongoMapper however there is an open ticket on it.
http://github.com/binarylogic/authlogic/issues#issue/15
There are many plugins I would like to see support MongoMapper. I encourage them to build in an adapter to deal with the different databases.
Devise does work with Mongo, I've been playing with it this week.
What I would love is a railscast on gridfs. I'm fairly new to rails and I got Devise to work with mongo and mongomapper, but gridfs is confusing me no end.
Good to know about MongoDB. I have a question though. Where is the data stored in mongodb? Is there any way to perform full text search and db backups, etc like traditional relational databases would allow us to do?
Also how is it different from storing data in xml files?
Ryan great screencasts!
What are the advantages of using mongodb over couchdb?
@paul gatt for mongodb and gridfs there is the new CarrierWave gem: http://github.com/jnicklas/carrierwave
There's also http://github.com/twoism/grip for gridFs.
RailsTips got an article released a few days ago about it -> http://railstips.org/2009/12/23/getting-a-grip-on-gridfs
How about performance (for example vs mysql)?
Thibaud and Fredrik, tried all those options.
I may just not know enough, but I couldn't get them to work. Saved the image to mongo, but couldn't pull it back out for love or money.
Ryan, what's reason for usage of "h" escape_html method within Rails 2.3.5?
By the way, MongoDB’s performance as compared to others
http://prajwal-tuladhar.net.np/2009/11/15/500/mongodbs-performance-as-compared-to-others-esp-couchdb/
Great screencast Ryan!
I just wanted to mention that there was also a screencast series recently done on MongoDB by Joon You (rubyhead) which can be found on http://teachmetocode.com.
Apologies for the shameless plug ;-)
Thanks a LOT for feeding us Awesome screencast! I can't wait for the next screencast that explains more advanced mongo mapper like callback, how to tackle many to many relation, and probably if there is any asynchronous way for mongodb using ruby.
Where is the clap of your finger?? I was expecting it !
@Paul
You need a Rack or Metal-library that can handle the serving on the view-side.
http://github.com/twoism/metal_grid_fs/
For example :)
@Paul
There's also a faster, but not equally flexible way of doing it.
http://fukamachi.org/wp/2009/11/20/rails-metal-mongodb-gridfs-access/
Code needs editing on the parts of what database it uses.
Script to install MongoDB on OS X using Homebrew and set it to automatically run: http://gist.github.com/265272
One note about the screencast: Tasks should have been embedded in the Projects collections, because a single task isn't shared among projects. This is the true Mongo way. MongoMapper supports embedded models in a very nice object-oriented way.
Hi Ryan!
I saw this used with Rackamole (http://rackamole.com/) Maybe you can look into Rackamole too! It's a great rack plugin for monitoring a rails application.
I'm not used to this kind of DBM, I mostly use SQL (MySQL and MSSQL) so I can't really see the big avantages of this kind of DBM...
How's the performance of MongoDB? Especially when it comes to fulltext searching?
I expected to hear some use cases for such databases. I don't suppose they perform as good as *SQL, and I guess they're only supposed to be used in certain situations? I found this was missing from the railscast.
Otherwise nice railscast.
Doesn't work for me. I can't create the scripts because of active record. How do I either turn off active record, or what do I put in databases.yml (currently it's blank) to make it stop wanting to connect to the db?
@Matthew as long as you use Mongomapper in your models there shouldnt be any calls to ActiveRecord, and therefore no problems with configurations.
How about if someone wants to visually change the data (I mean not from the console). Is that possible?
@Shreyans
You got for Mac OS X - http://www.apple.com/downloads/macosx/development_tools/mongohub.html
Dunno if it works though, aint running Mac. MongoHub doesnt seem to work properly in Win7
thanks Ryan.
by the way, which plugin do you use for the syntax highlighting??
Ryan; Love your screencast on mongodb. I just wanted to add, for those users who use macports, there is a simpler way of installing mongodb and making sure it's up to date. Use the sudo port install mongodb command and you are all set to go with mongo!
Thanks Ryan, and Happy New Year!
Is it intentional (or my personal failing) that prevents me from being able to fast-forward and skip around your episodes when viewed in iTunes? The thumb is effectively disabled.
OK, it was of course my problem. The in-episode fast forward (at the bottom inside the frame) still does not work, but the iTunes scrubber (top, green window) does as always. Strange, but nevermind.
There is a good and simple tutorial for isntalling mongodb, you wont need to compaile it
http://www.slideshare.net/scottmotte/mongodb-1825613
@Matthew: I have encountered the same problem. Running the console everything's fine but when trying to open the site in a browser passenger obviously tries to load ActiveRecord which then complains since no database is configured. After hours of trial and error I will give up on MongoMapper until it works fine.
Love your casts Ryan. Any chance of getting one for integrating NEO4J with RoR?
What does #{Rails.env} do?
Seems like the page on how to install mongoDB on OS X and set it to autostart no longer works:(
I will try the Homebrew recipe from @Mislav instead ;)
I was working on a similar example using Rails3.Beta and MongoMapper. The biggest difficulty I had was that the models no longer inherited from ActiveRecord, and it felt like I had to change things significantly to make things work.
For example I couldn't use "form_for @object" because it would complain that @object didn't have method 'foo' (in this case I believe it was missing 'class_name'). I could get form_tag to work, but the experience made me wonder how well mongomapper+mongodb will work as a straight replacement to AR.
Well, I re-watched your cast and noticed that you didn't have these problems with 2.3.x!! Hopefully I am doing something wrong on my end, but am going to try with 2.3.x right now..
Very exciting though, I'm hoping to use Rails+mongo for my next side project, thank you for covering it here.
A little followup: I went back and tried this with Rails 2.3.5 and everything worked much better. I did have problems with the generator scripts; it seems like they aren't too tolerant of other ORM's. Once I got everything laid out though it worked brilliantly.
I did have problems with my database.yml file though; In your cast you don't empty it, but if I left database.yml populated with the sqlite information it would barf when I started the server. An empty database.yml made things much better.
Is there support for has_many :through situations?
If you are having trouble with your app being unable to start because of activerecord/database.yml related errors, make sure to put this
config.frameworks -= [ :active_record]
in your environment.rb
Thank you, very helpful. I'd like to review a similar cast about Cassandra with Rails.
whole idea that complex software should "help" me doing things
If I attempt to use any generators (e.g. nifty_scaffold or scaffold) the following error is thrown:
uninitialized constant Rails::Generator::GeneratedAttribute::ActiveRecord
I have turned off active record in environment.rb via:
config.frameworks -= [:active_record]
My database.yml file is empty. Everything is configured as per the screencast/mongo docs.
I'm running Rails 2.3.8, mongo 1.0.7, mongo_mapper 0.8.3, and MongoDB 1.6.0.
If anybody else has run in to this error (and found a solution), please comment. Previous comments have alluded to similar problems.
The files could be created manually, however I'm interested as to why this is happening - active record should be disabled.
with Rails3 out, and MongoID becoming more and more popular, it would be great to do a RailsCast about MongoID
Now I can't use anything else ... Rails + mongoDB it's just awesome
How to migrate the _data_ with mongodb? Although I dont need to modify the schema anymore I have to adjust the data from time to time. Is there a mechanism to collect such "db-patches" which can modify existing data in a comprehensive manner in the different environments?
MongoDB great - could we see a post on Mongoid? Thanks for the great work!
I wanna try this, thanks for share Ryan
Finally, I tried out NoSQL.. thanks a lot Ryan.. I created a small blogpost myself to show how to use mongomapper in rails to use cloud database hosted by Mongolab..
The "any attribute as a permalink" link in the Show Notes has a period following the URL in the href... (http://railscasts.com/episodes/63-model-name-in-url.)
Fortunately, I was able to figure it out, though maybe not so many people would be so lucky. :-)
Hi Ryan!
Any plans to revise this with Rails 4 ?
Four years later I find this to be the most concise and most complete Getting Started with MongoMapper guide around! Thanks.