ruby - Rails 4 - destroy action deletes wrong record -


i doing ajax request rails passing in data id.

here ajax

function delete_availability(id) {     var id = id;      $.ajax({       type: "delete",       url: "/events/" + id,       statuscode: {          200: function() {           //alert("200");         },         202: function() {           //alert("202");         }       },       success: function(data) {          console.log('availability deleted');        },       error: function(xhr) {         alert("the error code is: " + xhr.statustext);       }     });   } 

my destroy action

def destroy      @event = event.find_by(params[:id]);      respond_to |format|       if @event.destroy         format.json {           render json: {}         }       end     end   end 

my event model has nothing in it

class event < activerecord::base  end 

the problem though rails receives correct id, when goes destroying, changes id , destroys next one.

here rails log:

processing eventscontroller#destroy */*   parameters: {"id"=>"66"}   event load (0.1ms)  select  "events".* "events" (66) limit 1    (0.0ms)  begin transaction   sql (0.2ms)  delete "events" "events"."id" = ?  [["id", 65]]    (2.4ms)  commit transaction completed 200 ok in 6ms (views: 0.1ms | activerecord: 2.8ms) 

anyone knows why?

you should use event.find(params[:id]) or event.find_by(id: params[:id]).

what happens code sql query finds every event - where (66) true record - , find_by takes first record set, , gets destroyed. id request doesn't matter.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -