Categoryminor(#75100) expected, got “22” which is an instance of String(#2940)

  Kiến thức lập trình

The error identified in the Title is being invoked when running the following
@component = Ccomponent.new(ccomponent_params)

The reponse obtained “22” was isolated with two different submissions:

Parameters: {"authenticity_token"=>"[FILTERED]", "ccomponent"=>{"shop_id"=>"1", "unit_um_id"=>"22", "shelf_life_target"=>"2"}, "commit"=>"submit"}

Parameters: {"authenticity_token"=>"[FILTERED]", "ccomponent"=>{"shop_id"=>"1", "unit_um_id"=>"", "shelf_life_target"=>"2"}, "commit"=>"submit"}

With the second case returning got "" in the error message.

So the issue is with the relationship for the unit_um_id variable.

The class is defined with two variables that are sourced from the same class.

  belongs_to :um, class_name: 'Categoryminor'
  belongs_to :unit_um_id, class_name: 'Categoryminor', optional: true

The error had not appeared before when the unit_um_id variable did not exist.
Thus it appears rails does not allow two variables to belong to the same class (I can see why: params are always strings and it is the model logic that will convert to an integer: so only one relationship is being processed by that logic here)

um is in lieu of unit of measure and both are needed on the class (consider a bottle whose capacity is 75 cl (1st um), but is purchase in bottles (units – 2nd um).

One could not create the relationship a prior and run an indexed search every time the variable is needed, but that is not very efficient.

How could this model be construed otherwise?

LEAVE A COMMENT