Weird update problem.. ID value being dropped any ideas why?

Ive been trying to get my forst rials program to update and delete records ina mysql database and I've been experiencing this weird problem where no errors are thrown up the data doent get updated/deleted.

Ive rtaced the problem down to the fact that the ID for the record to be updated or deleted is not being passed properly. But theer doesnt seem to be any way of making ti work correctly.

EG: from logfile

Processing UsersController#update (for 134.225.101.126 at 2008-06-12 12:15:42) [PUT]   Session ID: BAh7BzoMY3NyZl9pZCIlODNmNzY3YWRhMzQ5MWU3YjY5YmYyZDM5MTU4Nzc0%0ANzkiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh%0Ac2h7AAY6CkB1c2VkewA%3D--67019028005bd54bb3a0ff18a2c6acfb4ad8941b   Parameters: {"user"=>{"Staff"=>"1", "MscInfo"=>"0", "HdrForum"=>"0", "Internal_Sort"=>"", "Lab"=>"0", "Secs"=>"0", "UgBoard"=>"0", "Personal_Web"=>"~arrdvariusmaximus", "UgVis"=>"0", "ProbationDate"=>"", "LineManager"=>"", "BscInfo"=>"0", "PhdSuper"=>"1", "Security"=>"0", "SurName"=>"Aaardvark123", "HdrBoard"=>"0", "Research"=>"1", "Title"=>"Mr", "EMail"=>"a.aardvark@website.com", "ForeName"=>"Aaalan", "Year"=>"", "Cracks"=>"0", "InfoSec"=>"0", "RetirementDate"=>"", "PhoneNo"=>"1111", "Mats"=>"0", "Media"=>"1", "PhdInfo"=>"0", "ResearchGroup"=>"", "Position_Group"=>"Ardvar Asimilation", "Duties"=>"Arythmia", "Group_Web"=>"~ard", "Course"=>"", "Teapot"=>"0", "StartDate"=>"", "EndDate"=>"", "FundingBody"=>"", "ImageName"=>"noimage.gif", "RoomNo"=>"A102", "PayCategory"=>""}, "commit"=>"Update", "authenticity_token"=>"428c0c3d4d97925d4b6de702b6b65a30b58963b2", "_method"=>"put", "action"=>"update", "id"=>"1223", "controller"=>"users"}   e[4;36;1mUser Columns (0.013565)e[0m e[0;1mSHOW FIELDS FROM `users`e[0m   e[4;35;1mUser Load (0.011418)e[0m e[0mSELECT * FROM `users` WHERE (`users`.`id` = '1223') e[0m   e[4;36;1mSQL (0.000467)e[0m e[0;1mBEGINe[0m   e[4;35;1mUser Update (0.000980)e[0m e[0mUPDATE `users` SET `UgVis` = 0, `LineManager` = '', `Staff` = 1, `Position_Group` = 'Ardvar Asimilation', `Group_Web` = '~ard', `Duties` = 'Arythmia', `Title` = 'Mr', `UgBoard` = 0, `Secs` = 0, `Year` = '', `RetirementDate` = '', `MscInfo` = 0, `PayCategory` = '', `ImageName` = 'noimage.gif', `Security` = 0, `BscInfo` = 0, `Personal_Web` = '~arrdvariusmaximus', `Internal_Sort` = '', `Teapot` = 0, `FundingBody` = '', `EndDate` = '', `SurName` = 'Aaardvark123', `Research` = 1, `ResearchGroup` = '', `PhoneNo` = '1111', `PhdInfo` = 0, `Media` = 1, `Mats` = 0, `PhdSuper` = 1, `InfoSec` = 0, `ForeName` = 'Aaalan', `Cracks` = 0, `ProbationDate` = '', `RoomNo` = 'A102', `EMail` = 'a.aardvark@website.com', `Lab` = 0, `StartDate` = '', `Course` = '', `ID` = 1223, `HdrBoard` = 0, `HdrForum` = 0 WHERE `id` = NULLe[0m   e[4;36;1mSQL (0.000295)e[0m e[0;1mCOMMITe[0m Redirected to http://website/users/users/ Completed in 0.09493 (10 reqs/sec) | DB: 0.02672 (28%) | 302 Found [http://website/users/users/1223\]

As you can see in the first instance of generated SQL the ID is preserved

SELECT * FROM `users` WHERE (`users`.`id` = '1223'

but in the actual UPDATE line its getting WHERE `id` = NULL

Any ideas why this might be happening? I'm using MySQL on Apache with fastcgi

What'll really help people diagnose your issue is to see the controller code... it's a little tough to diagnose from just the output.

Given the log, you have the ID, but then lose it. Maybe you're not using an instance variable where you ought to?

Ar Chron wrote:

What'll really help people diagnose your issue is to see the controller code... it's a little tough to diagnose from just the output.

Given the log, you have the ID, but then lose it. Maybe you're not using an instance variable where you ought to?

ok here you go.

class UsersController < ApplicationController   # GET /users   # GET /users.xml   def index     if params[:usersearch].blank?     @users = User.find(:all, :order => 'SurName, ForeName')     else

        @users = User.find(:all, :order => 'SurName, ForeName', :conditions => ['Surname LIKE ? or Forename LIKE ? or Position_Group LIKE ?', "%#{params[:usersearch]}%", "%#{params[:usersearch]}%", "%#{params[:usersearch]}%"])   end

    respond_to do |format|       format.html # index.html.erb       format.xml { render :xml => @users }     end   end

  # GET /users/1   # GET /users/1.xml   def show     @user = User.find(params[:id])

    respond_to do |format|       format.html # show.html.erb       format.xml { render :xml => @user }     end   end

  # GET /users/new   # GET /users/new.xml   def new     @user = User.new

    respond_to do |format|       format.html # new.html.erb       format.xml { render :xml => @user }     end   end

  # GET /users/1/edit   def edit     @user = User.find(params[:id])   end

  # POST /users   # POST /users.xml   def create     @user = User.new(params[:user])

    respond_to do |format|       if @user.save         flash[:notice] = 'User was successfully created.'         format.html { redirect_to(@user) }         format.xml { render :xml => @user, :status => :created, :location => @user }       else         format.html { render :action => "new" }         format.xml { render :xml => @user.errors, :status => :unprocessable_entity }       end     end   end

  # PUT /users/1   # PUT /users/1.xml   def update     @user = User.find(params[:id])

    respond_to do |format|       if @user.update_attributes(params[:user])         flash[:notice] = 'User was successfully updated.'         format.html { redirect_to(@user) }         format.xml { head :ok }       else         format.html { render :action => "edit" }         format.xml { render :xml => @user.errors, :status => :unprocessable_entity }       end     end   end

  # DELETE /users/1   # DELETE /users/1.xml   def destroy     @user = User.find(params[:id])     @user.destroy

    respond_to do |format|       format.html { redirect_to(users_url) }       format.xml { head :ok }     end   end end

The update code looks simple enough, not much that can go wrong I think.

One thing I see is that the id field is sometimes called ID, sometimes id. All the other fieldnames are casesensitive, too. There may(!) be a source for confusion, depending on the db you use, since the uppercase id in the update still has the correct value, while it checks for the uppercase ID, which is NULL.

Otherwise any before_save filters or validations may be responsible.

Thorsten Mueller wrote:

The update code looks simple enough, not much that can go wrong I think.

One thing I see is that the id field is sometimes called ID, sometimes id. All the other fieldnames are casesensitive, too. There may(!) be a source for confusion, depending on the db you use, since the uppercase id in the update still has the correct value, while it checks for the uppercase ID, which is NULL.

Ive tried using all lower case "id" but if i use

<td><%= link_to 'Edit', edit_user_path(user.id) %></td>

instead

of

<td><%= link_to 'Edit', edit_user_path(user.ID) %></td>

The path renders incorrectly. EG: website/users/users//edit/ instead of website/users/users/id/edit/

Otherwise any before_save filters or validations may be responsible.

I don't have any filters set or validations in the model file at this point so it can be that.

This problem is really vexing me since the code looks like it should work, but even default scaffold code wont do updates or deletes but will happily create new records.

ok, I'm running out of ideas. The controller is simple enough, that must work...

How does your db look? What type is it? Oracle, MySQL? Some are case sensitive (Oracle), others not.

Is there a chance, you can change the name of the id column to lowercase? At least to test it and see, what happens.

This `ID` = 1223 is strange enough by itself, Rails should not try to update that col and a short glance in the output of my devlog shows, that it normally does not.

Is there a chance, you can change the name of the id column to lowercase? At least to test it and see, what happens.

Well isn't that weird, I changed ID to id using navicat (its a MySQL database) and update now works (after changing all the user.ID references to user.id). thanks a lot for kicking me in the brain.

Cheers and +1 for your help.

Although delete still doesn't work. It seems to route to the show page instead of deleting and doing a redirect to the index.

That's an easy one. The default path helper for both of them is singular_path(id)

The destroy action needs a :method => :delete wherever it's used

<%= link_to('verwijder', user_path(user.id), :confirm => 'Weet je het zeker?', :method => :delete) %>