# Unable to update database object

**URL:** <https://www.ruby-forum.com/t/unable-to-update-database-object/72310>\
**Category:** Rails\
**Created:** [February 23, 2006, 2:07pm UTC](https://www.ruby-forum.com/t/unable-to-update-database-object/72310 "2006-02-23T14:07:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [February 23, 2006, 2:07pm UTC](https://www.ruby-forum.com/t/unable-to-update-database-object/72310/1 "2006-02-23T14:07:24Z")

</div>

This is really strange - hopefully someone out there may have seen  
behavior like this before. I have a user successfully created in the  
Users table of my application. When I try to update a single column  
using:

@user = User.find(:first,  
:conditions =\> [“login\_name = ?”,  
@login\_user.login\_name])  
@user.new\_cookie\_key  
@user.save

I get the error: “NoMethodError in Users#login. You have a nil object  
when you didn’t expect it!”

The user is found OK, the new\_cookie\_key method works as expected, but I  
can’t save the object! I’ve removed all validation from the User model  
but that makes no difference. If I comment out the save method and debug  
the object using \<%= debug(@user) %\> in a View, the object and its  
contents are shown as expected, so it certainly isn’t a nil object!

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [February 23, 2006, 9:13pm UTC](https://www.ruby-forum.com/t/unable-to-update-database-object/72310/2 "2006-02-23T21:13:54Z")

</div>

I’ve dug a little deeper, and found this in the server log:

User Load (0.001551) SELECT \* FROM users WHERE (login\_name = ‘adam’  
) LIMIT 1  
SQL (0.000273) BEGIN  
User Load (0.001240) SELECT \* FROM users WHERE (login\_name = ‘adam’  
AND id \<\> 16) LIMIT 1  
SQL (0.000279) COMMIT

Weird thing is, “adam” row in the database has the id of 16! Why is  
rails selecting the users table with id != 16? Why is there no update  
before the commit?

Lindsay

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [February 23, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/unable-to-update-database-object/72310/3 "2006-02-23T21:39:21Z")

</div>

At 2/23/2006 03:13 PM, you wrote:

> rails selecting the users table with id != 16? Why is there no update  
> before the commit?
> 
> Lindsay

Is there a validate\_uniqueness\_of on the login\_name? I’d suspect  
that the second SQL would be to confirm that.  
-Rob

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [February 23, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/unable-to-update-database-object/72310/4 "2006-02-23T22:01:30Z")

</div>

> Is there a validate\_uniqueness\_of on the login\_name?

Oh, I see now… I’ve added an ‘:on =\> :create’ condition and this  
appears to have fixed that particular problem. Thanks!

Ok, my server log is now showing:

User Load (0.000913) SELECT \* FROM users WHERE (login\_name = ‘adam’  
) LIMIT 1  
SQL (0.000310) BEGIN  
SQL (0.000242) ROLLBACK

So somehow my @user.save method is still causing me grief!

Lindsay
