views:

101

answers:

1

I have a Course class that has many WeightingScales and I am trying to get the WeightingScales validation to propagate through the system. The short of the problem is that the following code works except for the fact that the errors.add_to_base() call doesn't do anything (that I can see). The Course object saves just fine and the WeightingScale objects fail to save, but I don't ever see the error in the controller.

  def weight_attributes=(weight_attributes)
    weighting_scales.each do |scale|
      scale.weight = weight_attributes.fetch(scale.id.to_s).fetch("weight")

      unless scale.save
        errors.add_to_base("The file is not in CSV format")
      end
    end
  end

My question is similar to this [1]: How can you add errors to a Model without being in a "validates" method?

link text

+1  A: 

If you want the save to fail, you'll need to use a validate method. If not, you'll have to use callbacks like before_save or before_create to check that errors.invalid? is false before you allow the save to go through. Personally, i'll just use validate. Hope it helps =)

Staelen