ruby - Rescuing an ActiveRecord::RecordNotUnique in Rails 3.2 -
i new ruby , want ensure combination of 3 columns in model unique. tried using validates_uniqueness_of scopes didn't work intended. added unique index db raises activerecord::recordnotunique exception every time existing combination of values saved. problem not sure on how handle exception. in controller altered update action to:
def update @patient_history = patienthistory.find(params[:id]) @patient_history.save! respond_to |format| format.html { redirect_to @patient_history, notice: 'patient history updated.' } format.json { head :no_content } end rescue activerecord::recordnotunique respond_to |format| format.html { render action: "edit" } format.json { render json: @patient_history.errors, status: :unprocessable_entity } end end end
this code not saves changes , redirects show page of model (:patient_history) notice "patient history updated". trying controller redirect edit page , flash message @ top of page error (much validates_uniqueness of do).
thanks in advance
how did try using validates_uniqueness_of? should use make sure combination unique, this:
validates_uniqueness_of :column1, :scope => [:column2, :column3]
Comments
Post a Comment