Mongoid parent child relationship

MongoDB: Relationships Basics – jambyjambalos

Rails does have accepts_nested_attributes_for for being able to save child attributes directly on the parent - but most people tend to stay away. What we will be setting up is a relationship of Recipes to Ingredients through the parent model (Recipe) to also modify and create entries in. include Mongoid::Document has_many:posts:autosave create a field in which to store or reference the posts in the parent object (*Board*).

Self referential relationships with mongoid

So, now that I got it working, I'm going to share how the heck to do it so you can learn from my guinea pigging. Application Summary In this example, we'll be setting up a Recipe book.

Recipes have multiple meta properties like their title, a brief description, instructions and the like. Each recipe of course needs ingredients and each ingredient needs quantities of that ingredient for the recipe. What we will be setting up is a relationship of Recipes to Ingredients through Quantities.

Setting Up Your Models recipe. This property will allow modification of the Recipe model updating and creating to also modify attributes of the associated Quantities records. This allows you to easily setup a relationship from one model to another with a join model between them to easily provide access to the models from each other.

Oracle SQL Tutorial 16 - Parent Child Relationships

In this case for example recipe. You can also define rejection and deletion controls for the relational model through this method.

See the official Active Record Nested Attributes documentation for more information. This allows modification of the join model Quantity through the parent model Recipe to also modify and create entries in the final relational model Ingredient. Note that the mass-assignment white-list on the join model also includes its own attribute of an: This allows easy access to the recipes that an ingredient is associated to via simple ingredient. Installing Cocoon to make nested forms easy-peasy To make setting up a nested form for a Recipe that can have 1 or more Ingredients each with a Quantity easy, we use a little gem called Cocoon.

Self referential relationships with mongoid

Cocoon provides drop-in JavaScript functionality to easily add and remove multiple entries of Ingredients to your nested form. Now let's say we want our app to allow users to update a Location and its address types, including deleting one or the other. How do we make sure that a Location can't be left without any type of address at all? You would think that the presence validations that we defined in the Location model would be enough to protect us, but we'd be in for a bad surprise if we used the typical destroy controller action that calls.

Let's try it in the console: This actually feels like a Rails bug to me, especially given this feature that was added 2 years ago, which, if I understand it correctly, should be preventing this scenario. One way to keep a Location from losing both of its addresses is to use nested forms.

[mongoid] what is the best way for reference child's id in parent object in mongoid?

If you're not familiar with nested forms, I recommend watching Railscasts Basically, instead of having a separate controller for each address type, and deleting an Address via its own form by calling. To delete an address, you could add a checkbox with a: But what if we didn't want to use nested forms, or what if we were building a RESTful API that would allow clients to delete an Address via a separate endpoint?

The key is to understand what Rails does when you remove an object via a nested form. If you looked at the server logs after submitting the nested form, you would see something like this: