2

I have a form set up for a Recipes model that accepts_nested_attributes_for Ingredients. The form and saving works fine, however, I want to check for the existence of each Ingredient in the database. The form seems to be creating new records for ingredients, even if they already exist in the database.

Recipes and Ingredients are related using a 'has_many :through' relationship. Is there a clean way to make sure that ingredients are duplicated when the form is submitted, but the relationships are still established?

Christian Benincasa
  • 1,215
  • 1
  • 21
  • 45

2 Answers2

0

similar question might help you on this, although the solution is not that eligant

Rails nested form on many-to-many: how to prevent duplicates?

Community
  • 1
  • 1
Zed
  • 113
  • 9
0

It is about how you are taking input from the user - your forms are. If you have a multiple select input for ingredients in new/edit form of recipe and you show all the existing ingredients there, user can choose the existing ingredients and the POST/PUT from these forms will have the existing ingredients' ids as part of form data. This will not create new ingredients then.

Salil
  • 9,534
  • 9
  • 42
  • 56