Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice Mongoid:NestedResources

I'm starting off with a small Rails3:MongoDB:Mongoid project and came along some questions that's nature is more architectural.

When to use nested resources, and how deep to nest?

I'm not a friend of nested routes at all, but they become handy if not stacked deeper than 2 resources and document oriented databases like MongoDB seem to be a perfect target for nesting at all since documents itself are nested.

So my first question was: Should I use nested routes in case of a references_many <=> referenced_in association?

I tried, and since ActiveRecord's "build" is not existant, the whole controller stuff brake down to something that looked much nicer without nesting at all and doing all the stuff manually.

Am I wrong? Should I nest the routes of such associations too?

After that I came to my first embedded documents, and argued myself: Hey, this begs for nesting the routes. Some iterations later I ended up with 4 instead of 2 documents, every embedded in one of the others. So nesting the routes led to URL's I did'nt really like.

So the question to the already MongoDB-experienced user is: should I nest? And if yes, under what circumstanced and how deep?

Greets J.

like image 328
Jason Nerer Avatar asked Sep 19 '26 17:09

Jason Nerer


1 Answers

I had a similar set of questions. Ryan Bates created an excellent Railscast about Mongoid after you made this post. There is a section covering associations and when to use the embedded_* idiom versus the references_* idiom. It has examples of both. Hopefully you've had a chance to check out the Railscast. If not, here is the link to the associated ASCIIcast:

http://asciicasts.com/episodes/238-mongoid

Everything I've read about nesting resources with respect to Mongoid, or ActiveRecord states a best practice of nesting no more than one level deep.

http://weblog.jamisbuck.org/2007/2/5/nesting-resources

like image 120
Mike Clymer Avatar answered Sep 22 '26 11:09

Mike Clymer