I have a simple form submission.
On Model I have added
validates_uniqueness_of :field
But somehow in my Database I get records with duplicate value of field.
And Controller Methods are as Follows
def create
field_obj=Model.find_by_field(params[:model][:field])
if field_obj.nil?
@model = Model.new(params[:model])
if (@model.save && params[:commit] == "submit")
@model.is_submitted=true
@model.submitted_timestamp=Time.now
@model.save
elsif (@model.save(:validate => false) && params[:commit] == "save")
@model.last_saved_timestamp=Time.now
@model.save
end
respond_to do |format|
if (@model.save || (@model.save(:validate => false)))
format.json { render json: @model, status: :created, location: @model }
format.js
else
format.js
format.json { render json: @model.errors, status: :unprocessable_entity }
end
end
else
@model = Model.find(field_obj.id)
if (@model.update_attributes(params[:model]) && params[:commit] == "submit")
@model.is_submitted=true
@model.save
end
respond_to do |format|
if (@model.update_attributes(params[:model])|| (@model.save(:validate => false)))
format.js
format.html { redirect_to @model, notice: 'Model was successfully updated.' }
format.json { head :no_content }
else
format.js
format.html { render action: "edit" }
format.json { render json: @model.errors, status: :unprocessable_entity }
end
end
end
end
def update
@model = Model.find(params[:id])
if (@model.update_attributes(params[:model]) && params[:commit] == "submit")
@model.is_submitted=true
@model.submitted_timestamp=Time.now
@model.save
elsif @model.update_attributes(params[:model], :validate=>false) && params[:commit] == "save"
@model.save
end
respond_to do |format|
if (@model.update_attributes(params[:model])|| (@model.save(:validate => false)))
format.js
# format.html { redirect_to @model, notice: 'Model was successfully updated.' }
format.json { head :no_content }
else
format.js
format.html { render action: "edit" }
format.json { render json: @model.errors, status: :unprocessable_entity }
end
end
end