Typical config/setup without Rails, and other questions

212 views
Skip to first unread message

Darren Schnare

unread,
Apr 21, 2012, 12:43:05 PM4/21/12
to mongo...@googlegroups.com
Hey guys,

I love MongoMapper, but as a new Ruby user I'm finding it challenging to find information regarding proper configuration/setup. I'm not interested in using Rails, so I'm looking for documentation on how to get started without Rails. I've tried digesting the source code on GitHub, but this is about as fun as trying to shave with a butter knife =)

If anyone can help me out with the following it would be greatly appreciated:

1) What files are required/recommended for configuration (database.yml, mongo.yml, mongo_mapper.yml, etc.) and where should those files be located?
2) Now that :index is deprecated for keys, what is the best practice for creating indexes (i.e. Rake, class method in the model, db/indexes.rb -- where/how does this get loaded? etc.)?
3) What are all the ways to create/delete documents (i.e. new, create, create!, save, save!, delete, destroy, etc.) and what are the pre/post conditions of each method? I understand most of these already but if I can get the definitive documentation on this then that would great. 
4) I've started using the documentation on Yard, but there don't seem to be any documentation for any of the methods. Is there a better place to find this information?

Thanks in advance! Keep up the great work!

Kevin Lawver

unread,
Apr 22, 2012, 11:28:33 AM4/22/12
to mongo...@googlegroups.com
The most important setup step is setting MongoMapper.connection.  It should look something like this:

MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "myapp_development"

If you're connecting to a replica set, that's going to look a little different, but that should get you started.  That should happen in your code after requiring mongo_mapper but before you try to use it.

If you're not using Rails or have a lot of complex configuration, then that will do you.  Other answers below...

On Apr 21, 2012, at 12:43 PM, Darren Schnare wrote:

Hey guys,

I love MongoMapper, but as a new Ruby user I'm finding it challenging to find information regarding proper configuration/setup. I'm not interested in using Rails, so I'm looking for documentation on how to get started without Rails. I've tried digesting the source code on GitHub, but this is about as fun as trying to shave with a butter knife =)

If anyone can help me out with the following it would be greatly appreciated:

1) What files are required/recommended for configuration (database.yml, mongo.yml, mongo_mapper.yml, etc.) and where should those files be located?
2) Now that :index is deprecated for keys, what is the best practice for creating indexes (i.e. Rake, class method in the model, db/indexes.rb -- where/how does this get loaded? etc.)?

ensure_index is there.  I usually create a method in each class called self.create_indexes that has all my ensure_index calls.  That's probably not the best way, but it works for me.

3) What are all the ways to create/delete documents (i.e. new, create, create!, save, save!, delete, destroy, etc.) and what are the pre/post conditions of each method? I understand most of these already but if I can get the definitive documentation on this then that would great. 

It all depends on what you need.  I usually use create, which will return 'false' if it fails.  create! raises an error if it fails. The docs have descriptions of the callbacks: http://mongomapper.com/documentation/plugins/callbacks.html

4) I've started using the documentation on Yard, but there don't seem to be any documentation for any of the methods. Is there a better place to find this information?

Darren Schnare

unread,
Apr 22, 2012, 1:47:44 PM4/22/12
to mongo...@googlegroups.com
Thanks for your help Kevin.

RE: Config/setup:
I'm currently using some boilerplate code from the Ruby Grape project on GitHub ( https://github.com/intridea/grape ) to configure MongoMapper. It's working well, but I have no idea what configuration settings are even available for MongoMapper. For the time being I'll just leave it as is.

RE: Indexes
The technique you describe does indeed seem to be the best approach I've seen so far. I've come across a post describing a technique that leverages the class-method approach and Rake to ensure indexes are created: http://technicaldebt.com/?p=1089. I'll likely do something like you suggest and what is in the post I mentioned.

RE: Document creation
This makes sense, thanks for the help. Don't know why I didn't quite get it the first time I read the source code.

RE: Documentation
I'm familiar with the documentation pages on mongomapper.com, but they are more of a reference/guide than the documentation I was looking for. No worries, I'll suck it up and continue reading through the source.

Brian Hempel

unread,
Apr 22, 2012, 3:06:17 PM4/22/12
to mongo...@googlegroups.com
> 1) What files are required/recommended for configuration (database.yml, mongo.yml, mongo_mapper.yml, etc.) and where should those files be located?


I started some doc updates for configuring MongoMapper...~75% complete. Should probably finish those...

Brian

Jon Kern

unread,
Apr 26, 2012, 8:39:20 AM4/26/12
to mongo...@googlegroups.com
I don't have the definitive answer... and the examples are a bit dated... but you can see some free-standing ruby/MM/mongo examples described on my blog and you can see example code on my github: https://github.com/JonKernPA/mongo_examples.
jon

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA

Brian Hempel said the following on 4/22/12 3:06 PM:
Reply all
Reply to author
Forward
0 new messages