# Update attributes

**URL:** <https://www.ruby-forum.com/t/update-attributes/85849>\
**Category:** Rails\
**Created:** [February 16, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/update-attributes/85849 "2007-02-16T19:44:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![namwob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@namwob](https://www.ruby-forum.com/u/namwob)\
**Post date:** [February 16, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/update-attributes/85849/1 "2007-02-16T19:44:47Z")

</div>

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”}  
[4;36;1mUser Load (0.000761) [0m [0;1mSELECT \* FROM users WHERE  
(users.id = ‘11’) LIMIT 1 [0m  
[4;35;1mUser Columns (0.002650) [0m [0mSHOW FIELDS FROM  
users [0m  
[4;36;1mSQL (0.001330) [0m [0;1mBEGIN [0m  
[4;35;1mUser Load (0.000497) [0m [0mSELECT \* FROM users WHERE  
(users.name = ‘joseph’ AND users.id \<\> 11) LIMIT 1 [0m  
[4;36;1mSQL (0.000096) [0m [0;1mCOMMIT [0m  
Redirected to [http://localhost:3000/admin/edit\_user](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](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 1

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:

# Update User

\<%= 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’ %\>

| Username: | \<%= text\_field("user","name") %\> |
| Password: | \<%= password\_field("user","hashed\_password") %\> |
| User Type: | \<%= select("user","user\_type", %w{nelnet wsu}) %\> |
| User Role: | \<%= select("user","user\_role", %w{user super admin}) %\> | |

Any help would be great

---

<div class="post-metadata">

**Author:** ![namwob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@namwob](https://www.ruby-forum.com/u/namwob)\
**Post date:** [February 16, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/update-attributes/85849/2 "2007-02-16T20:02:50Z")

</div>

On 2/16/07, namwob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

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

---

<div class="post-metadata">

**Author:** ![namwob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@namwob](https://www.ruby-forum.com/u/namwob)\
**Post date:** [February 16, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/update-attributes/85849/3 "2007-02-16T20:05:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![namwob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@namwob](https://www.ruby-forum.com/u/namwob)\
**Post date:** [February 16, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/update-attributes/85849/4 "2007-02-16T20:07:50Z")

</div>

what exact line is causing rails to throw that error?

---

<div class="post-metadata">

**Author:** ![namwob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@namwob](https://www.ruby-forum.com/u/namwob)\
**Post date:** [February 16, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/update-attributes/85849/5 "2007-02-16T20:10:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![namwob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@namwob](https://www.ruby-forum.com/u/namwob)\
**Post date:** [February 16, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/update-attributes/85849/6 "2007-02-16T20:11:51Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![namwob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@namwob](https://www.ruby-forum.com/u/namwob)\
**Post date:** [February 16, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/update-attributes/85849/7 "2007-02-16T23:48:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![namwob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@namwob](https://www.ruby-forum.com/u/namwob)\
**Post date:** [February 16, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/update-attributes/85849/8 "2007-02-16T23:17:20Z")

</div>

Can someone explain why “validates\_presence\_of” screws up a call to  
update a model object?
