AssociationTypeMismatch with the expected type in nested model forms

I get this exception when creating a nested model:

ActiveRecord::AssociationTypeMismatch in RecipesController#update 
  Ingredient(#35624480) expected, got Ingredient(#34767560)

Models include recipe and ingredient. The recipe has_many and accepts_nested_attributes_for: ingredients that belong to the recipe.

I get this exception when _destroy (= 1) tries one of the pre-existing ingredients in the Ingredient sub-form for Editing / updating the recipe.

This makes little sense, mainly because the types of associations are as expected (with the exception of their own tolerance).

Even less important, it works great in a functional test .

In addition, it sometimes works if I re-post the form (via a browser update on Update). It will also work if I restart the development (development) web server.

Any ideas that might be causing this, or what should I look for?

+3
source share
2 answers

this usually means that "rails could not restart successfully once" in the / xxx environment files you can change it so as not to reboot, or allow reboot.

+6
source

This can sometimes be fixed by adding the correct require request, beginning where the class is used in your code

-1
source

Source: https://habr.com/ru/post/1746908/


All Articles