ruby-on-railsvalidationmodelafter-save

How can I reject the record which not passed condition on model


I have a Car model which i want to check and discard record before it's saved ,if it not pass the condition from method the code is look like this :

class Car < ActiveRecord::Base
  after_save :wheel_not_zero
  def wheel_not_zero
   false if self.wheel_num == 0 
  end 
end

by this solution it'll reject all of car that user send from the form with error and redirect to form again. I want to reject only car which match a condition with out redirect to form again.

I create car record via nested attribute from member model the relation is look like this

member -> car ,so I want to save member record and not matched condition cars record, right now it's reject all records.

thank you for all response :)


Solution

  • The redirection is not from the model but is from the controller to the view. So if you want to discard the record you should use a validator and in the controller you should manage the exception. For example: if the record is invalid you can redirect to the home page or in an other page..

    The important thing is keeping in mind that the redirection is separate from the model