As @Pavan said in the comment, your conditional might be wrong. It should be ==
instead of =
. =
is the assignment.
if @weight.nominal + x == required ... end
Here what happens “behind the scene” with your original code:
required
is assigned tox
.- Add x to
@weight.nominal
(@weight.nominal + x
) - Evaluate the result from step 2, which is always true.
- Execute the code within
if
block, which isweights << @weight.id
Eventually, your x
‘s value is lost, it will take required
value instead and your conditional is useless since it always true.
solved What is wrong with my if statement ‘if x + y = z’