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