Update attributes

I am a bit confused as to why I am getting an error updating an object.

This is the ERROR: Couldn't find User without an ID

In my development.log file I noticed the following when trying to initiate the update:

Processing AdminController#update_user (for 127.0.0.1 at 2007-02-16 13:20:23) [POST]   Session ID: 2a5030c37fa9726e6e29d7a024ce97ab   Parameters: {"user"=>{"name"=>"joseph", "hashed_password"=>"3708cf23bf5bcd14a2383a4fb24c4af1fb4fb352", "user_type"=>"nelnet", "user_role"=>"user"}, "commit"=>" Update User ", "action"=>"update_user", "id"=>"11", "controller"=>"admin"}   e[4;36;1mUser Load (0.000761)e[0m e[0;1mSELECT * FROM users WHERE (users.id = '11') LIMIT 1e[0m   e[4;35;1mUser Columns (0.002650)e[0m e[0mSHOW FIELDS FROM userse[0m   e[4;36;1mSQL (0.001330)e[0m e[0;1mBEGINe[0m   e[4;35;1mUser Load (0.000497)e[0m e[0mSELECT * FROM users WHERE (users.name = 'joseph' AND users.id <> 11) LIMIT 1e[0m   e[4;36;1mSQL (0.000096)e[0m e[0;1mCOMMITe[0m Redirected to http://localhost:3000/admin/edit_user Completed in 0.01380 (72 reqs/sec) | DB: 0.00533 (38%) | 302 Found [http://localhost/admin/update_user/11\]

What I find most confusing is this line:

SELECT * FROM users WHERE (users.name = 'joseph' AND users.id <> 11) LIMIT 1e

I am confused why Rails would make this call with update_attributes. I am lost.

Any help would be greatly appreciated!

Thanks,

-E

Here is snippets of my Admin controller code that is in question:

  def edit_user

    @user = User.find(params[:id])

  end

   def update_user

    @user = User.find(params[:id])

       if @user.update_attributes(params[:user])          flash[:notice] = "User #{@userUpdate.name} updated successfully"          redirect_to(:action =>:list_users)        else          flash[:notice] = "Unable to process change, please try again"          redirect_to(:action =>:edit_user)       end

   end

Here is the edit_user.rhtml file:

<h1>Update User</h1>

<%= start_form_tag :action => 'update_user', :id => @user %>   <%= render :partial => 'form' %>   <%= submit_tag " Update User " %> <%= end_form_tag %>

<%= link_to 'Back', :action => 'list_users' %>

Here is the _form.rhtml file:

<%= error_messages_for 'user' %>

<!--[form:call]-->

<table> <tr>

   <td>Username:</td>    <td><%= text_field("user","name") %></td> </tr>

<tr>    <td>Password:</td>    <td><%= password_field("user","hashed_password") %></td> </tr>

<tr>    <td>User Type:</td>    <td><%= select("user","user_type", %w{nelnet wsu}) %></td> </tr>

<tr>    <td>User Role:</td>    <td><%= select("user","user_role", %w{user super admin}) %><td> </tr> <table>

<!--[eoform:call]-->

Any help would be great

before you start getting to the strange SQL call, try and find out why an ID isn't being passed. At what line do you receive this error? At the User.find(params[:id]) ? If so, find out why you're not getting a value for an id here, then try to figure out the next part.

Mike

The id does get passed. Note from my dev.log file the parameters getting passed:

Parameters: {"user"=>{"name"=>"joseph", "hashed_password"=>"3708cf23bf5bcd14a2383a4fb24c4af1fb4fb352", "user_type"=>"nelnet", "user_role"=>"user"}, "commit"=>" Update User ", "action"=>"update_user", "id"=>"11", "controller"=>"admin"}

id is listed, so it makes it to the controller.

what exact line is causing rails to throw that error?

Here is the application TRACE:

/Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/ i386/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/ base.rb:939:in `find_from_ids' /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/ i386/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/ base.rb:382:in `find' #{RAILS_ROOT}/app/controllers/admin_controller.rb:45:in `edit_user' /Applications/Locomotive2/Bundles/standardRailsSept2006.locobundle/ i386/bin/mongrel_rails:18

The issue that I can't grasp is why this call was made in my log file for a simple update call

SELECT * FROM users WHERE (users.name = 'joseph' AND users.id <> 11)

Can someone explain why "validates_presence_of" screws up a call to update a model object?

what you're doing looks fine, and the database query is definitely strange, but somewhere along the line, rails is executing a find without an ID.

The application trace doesn't show what line in your code is causing the error.

Using validates_presence_of shouldn't have any side effect, unless you're modifying it with an "if" condition and introducing an error at that point.

Mike