Forum: Ruby on Rails Unable to update database object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ae03102311939454d2b7f1ca3139fd97?d=identicon&s=25 Lindsay Boyd (Guest)
on 2006-02-23 14:07
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!
Ae03102311939454d2b7f1ca3139fd97?d=identicon&s=25 Lindsay Boyd (Guest)
on 2006-02-23 21:13
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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-02-23 21:39
(Received via mailing list)
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
Ae03102311939454d2b7f1ca3139fd97?d=identicon&s=25 Lindsay Boyd (Guest)
on 2006-02-23 22:01
> 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
This topic is locked and can not be replied to.