Revisiting fields and nested attributes in rails
I have been working more on Recetera lately, and I wanted to provide a bit of an update on what I’ve been doing. This mostly has to do with all I’ve learned and ran into when working with nested attributes and fields.
Since my last post regarding the recipe app, I realized I would like a separate model altogether for ingredients so that these can later be searchable and used elsewhere within the app. I created an
Ingredient model with
name attributes. This belongs to
Recipe which can use the attributes for
Ingredient. My models are below:
accepts_nested_attributes_for :ingredients, allow_destroy: true makes it possible for
Recipe to save with attributes belonging to
Ingredient as well as allows for these nested attributes to be deleted independently of the associated model. It is worth noting that when generating the model for
Ingredient, I made sure to include
recipe_id:integer as an attribute. This allows for the association to occur between the models.
When building the form to use for a new recipe, I created a separate set of fields impacting the
Ingredient model like so:
In the ingredients partial:
With this set up, when a user submits the form, the ingredients attributes
:name both become associated witht he
recipe_id of the new recipe we are creating. I actually ran into a bit of difficulty getting the association for work, but this is ultimately what worked with me after playing around with it for a while.
If you wish to review the source, it is available on github. It is a work in progress but please check it out here .