# Update\_attributes

**URL:** <https://www.ruby-forum.com/t/update-attributes/86230>\
**Category:** Rails\
**Created:** [February 20, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/update-attributes/86230 "2007-02-20T20:47:51Z")\
**Posts on this page:** 4\
**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 20, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/update-attributes/86230/1 "2007-02-20T20:47:51Z")

</div>

I am trying to do a simple update to a model. I think that I am  
making the call correctly, however the update is not made. Looking at  
the development.log, I see that when I try to make an  
update\_attributes call, the following is SQL call is made:

SELECT \* FROM users WHERE (users.name = ‘sara’ AND users.id \<\> 9)  
LIMIT 1

Since the users.name does = ‘sara’ and the users.id is ‘9’ the update  
query does not run.

My question is WHY DOES THIS SELECT QUERY get run in the first place

Here is my controller code for running the update:

def update\_user

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

if @update.update_attributes(params[:user])
  flash[:notice] = "User #{params[:user][:name]} updated

```

successfully"  
else  
flash[:notice] = “Error in updating record”  
end

```
redirect_to(:action => 'list_users')

```

end

and the entire log entry when this controller is called:

Processing AdminController#update\_user (for 127.0.0.1 at 2007-02-20  
12:25:31) [POST]  
Session ID: 236e817b752befbe3c15e1c78e8a3596  
Parameters: {“user”=\>{“name”=\>“sara”, “user\_type”=\>“nelnet”,  
“user\_role”=\>“super”}, “commit”=\>" Update User ",  
“action”=\>“update\_user”, “id”=\>“9”, “controller”=\>“admin”}  
[4;36;1mUser Load (0.000452) [0m [0;1mSELECT \* FROM users WHERE  
(users.id = ‘9’) LIMIT 1 [0m  
[4;35;1mUser Columns (0.002539) [0m [0mSHOW FIELDS FROM  
users [0m  
[4;36;1mSQL (0.001285) [0m [0;1mBEGIN [0m  
[4;35;1mUser Load (0.000522) [0m [0mSELECT \* FROM users WHERE  
(users.name = ‘sara’ AND users.id \<\> 9) LIMIT 1 [0m  
[4;36;1mSQL (0.000094) [0m [0;1mCOMMIT [0m  
Redirected to [http://localhost:3000/admin/list\_users](http://localhost:3000/admin/list_users)  
Completed in 0.01411 (70 reqs/sec) | DB: 0.00489 (34%) | 302 Found  
[[http://localhost/admin/update\_user/9](http://localhost/admin/update_user/9)]

---

<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 20, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/update-attributes/86230/2 "2007-02-20T21:10:57Z")

</div>

namwob wrote:

> My question is WHY DOES THIS SELECT  
> QUERY get run in the first place

Have you got any validations in the model?

---

<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:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/update-attributes/86230/3 "2007-09-25T23:03:33Z")

</div>

Further note:

If I comment out this line in my user.rb model file

validates\_uniqueness\_of :name

#validates\_presence\_of :name, :password

The SQL query in question still runs, but then the UPDATE query runs  
successfully.

I am totally lost as to the reason behind this. Any thoughts would be  
much appreciated.

Thanks,

-E

---

<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:** [September 25, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/update-attributes/86230/4 "2007-09-25T23:07:41Z")

</div>

Messages were delivered out of sequence.

namwob wrote:

> If I comment out this line in my user.rb model file
> 
> validates\_uniqueness\_of :name

I think it’s because an update is a save and validates\_uniqueness\_of is  
just  
doing what you asked it to do which is to disallow a save if a record  
already exists that meets that criteria. Maybe it should be smarter.  
In  
the meantime, you can add an :except condition to your validation for  
the  
update.

hth,  
Bill
