All pastes #105864 Raw Edit

Find problem

public ruby v1 · immutable
#105864 ·published 2006-07-29 23:13 UTC
rendered paste body
I've got a 'delete' method in my controller which deletes an object based on its 'id' which is passed as a param (i.e. http://example.com/delete/2).  If I use a dynamic finder (i.e. find_by_id(params[:id])) and params[:id] is nil (i.e. http://example.com/delete) -- an object, the last that was created, is found and deleted -- not what I would expect.  If I repeat the same find query, the second time 'nil' is returned -- the right result, but why do I have to run the query twice?  def save  return unless request.post?  @contact = Contact.new(params[:contact])  @contact.user = current_user  if @contact.save    flash[:note] = "Contact successfully created"    redirect_to(:controller => 'contacts', :action => 'list')  else    render :action => "create"  end  enddef delete    #need to do this twice, otherwise returns an object even though params[:id] is nil    #contact = Contact.find_by_id_and_user_id(params[:id], current_user.id)    contact = Contact.find_by_id_and_user_id(params[:id], current_user.id)    unless contact      flash[:note] = "Contact not found"      redirect_to(:action => 'list')      return    end    if contact.destroy      flash[:note] = "Contact deleted"      redirect_to(:action => 'list')    else      render :action => 'list'    end   end