Unable to update database object


#1

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!


#2

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


#3

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


#4

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