Uniqueness Not Maintained On MYSQL Database Rails 3.2 -


i have simple form submission.

on model have added

validates_uniqueness_of :field 

but somehow in database records duplicate value of field.

and controller methods 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 |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 |format|         if (@model.update_attributes(params[:model])|| (@model.save(:validate => false)))           format.js           format.html { redirect_to @model, notice: 'model 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 |format|       if (@model.update_attributes(params[:model])|| (@model.save(:validate => false)))         format.js         # format.html { redirect_to @model, notice: 'model 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 

i think rails 3.2 activemodel validation defind as

validates :field, uniqueness: true

thanks


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -