# Nil error on ActiveRecord::save!

**URL:** <https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585>\
**Category:** Rails\
**Created:** [December 30, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585 "2007-12-30T16:10:00Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [December 30, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/1 "2007-12-30T16:10:00Z")

</div>

Hi,

I am trying to save an ActiveRecord object, and I am getting a strange  
error - I checked and all of the required fields of the objects seem to  
be present. Here is what I try to do:

self.user.save!

And the error is:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.+

Do you have an idea what it may be? When I put a debug statement right  
before, and examined self.user, it showed as if it’s ok:

#\<User:0xb70ea1e8 @attributes={“salt”=\>“L8Qymoyol2O7rA==”,  
“updated\_at”=\>“2007-12-30 09:58:06”, “details”=\>“Valentin Chilaru, New  
York NY”, “lock\_version”=\>nil,  
“password\_salt”=\>“bf99b345ef5e9c70c9afeb9dbdebdaed9b09ca3f41bb714c749e7aac3647f989”,  
“id”=\>“3”, “exposure”=\>“0”, “first\_name”=\>“Valentin”,  
“login”=\>“vchilaru”, “created\_at”=\>“2007-12-30 09:58:06”,  
“balance”=\>“1000”, “active”=\>“1”}\>

Thanks

stacktrace:

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:59:in  
`transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/transactions.rb:95:in`transaction’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/transactions.rb:121:in  
`transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/transactions.rb:133:in`save!’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/associations/association\_proxy.rb:123:in  
`send' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/associations/association_proxy.rb:123:in`method\_mis

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [December 31, 2007, 6:38pm UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/2 "2007-12-31T18:38:12Z")

</div>

On 12/30/07, Victor Al [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You have a nil object when you didn’t expect it!  
> You might have expected an instance of Array.  
> The error occurred while evaluating nil.+
> 
> Do you have an idea what it may be?

Not really but you can test stuff before trying to save it:

self.user.save unless self.user.nil?

You can’t access user.errors to find the validation that failed if  
user doesn’t first exist.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [December 31, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/3 "2007-12-31T18:57:08Z")

</div>

Thanks, but as I said in my post, the user does exists (I have included  
the user object as shown by ruby-debugger).

Greg D. wrote:

> On 12/30/07, Victor Al [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > You have a nil object when you didn’t expect it!  
> > You might have expected an instance of Array.  
> > The error occurred while evaluating nil.+
> > 
> > Do you have an idea what it may be?
> 
> Not really but you can test stuff before trying to save it:
> 
> self.user.save unless self.user.nil?
> 
> You can’t access user.errors to find the validation that failed if  
> user doesn’t first exist.
> 
> –  
> Greg D.  
> [http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [January 2, 2008, 11:43am UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/4 "2008-01-02T11:43:06Z")

</div>

On 31 Dec 2007, at 17:57, Victor Al wrote:

> Thanks, but as I said in my post, the user does exists (I have  
> included  
> the user object as shown by ruby-debugger).  
> There should be a backtrace in the logs/console when your error  
> occurs. That should guide you on your way.

Fred

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [January 2, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/5 "2008-01-02T20:11:05Z")

</div>

How is the current object (self) associated with the user object? The  
references to AssociationProxy in the stacktrace may be pointing in  
that direction (malformed has\_many/habtm/etc).

AndyV

On Dec 30 2007, 10:10 am, Victor Al [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [January 3, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/6 "2008-01-03T18:46:10Z")

</div>

I think you are right - this seems to me to be the only explanation for  
this cryptic error about which I cannot see any more details. Thanks a  
lot for suggesting it. I have a very simple polymorphic association set  
up

activity.rb  
belongs\_to :transactionable, :polymorphic=\>:true  
belongs\_to :user

user.rb  
has\_many :activities

and finally

transfer.rb  
has\_one :activity, :through=\>:transactionable

So there is no fancy association between user and activity, just  
belongs\_to/has\_many.  
I am simply trying to get to the user from the activity with  
activity.user.save! The polymorphic association is not even involved  
here.

I know see one of the major drawbacks of Ruby as a language: very poor  
error messages.

Thanks,  
V

AndyV wrote:

> How is the current object (self) associated with the user object? The  
> references to AssociationProxy in the stacktrace may be pointing in  
> that direction (malformed has\_many/habtm/etc).
> 
> AndyV
> 
> On Dec 30 2007, 10:10 am, Victor Al [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [January 4, 2008, 12:09am UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/7 "2008-01-04T00:09:00Z")

</div>

> I know see one of the major drawbacks of Ruby as a language: very poor  
> error messages.

I’m going to have to disagree with you there. The error messages are  
extremely detailed and the backtrace lets you see which files called  
what  
method on what line (unlike certain other languages).

Can you show us the complete method that you’re calling?

You could also try user.save! without the self.

–  
Ryan B.  
[http://www.frozenplague.net](http://www.frozenplague.net)  
Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [January 4, 2008, 5:52am UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/8 "2008-01-04T05:52:32Z")

</div>

Sure, here is the full error:

NoMethodError in ActivitiesController#new

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.+

RAILS\_ROOT: script/../config/..  
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/locking/optimistic.rb:64:in  
`update_without_callbacks' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/callbacks.rb:267:in `update\_without\_timestamps’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/timestamp.rb:39:in  
`update' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:1792:in `create\_or\_update\_without\_callbacks’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/callbacks.rb:242:in  
`create_or_update' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:1554:in `save\_without\_validation!’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/validations.rb:762:in  
`save_without_transactions!' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/transactions.rb:133:in `save!’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:59:in  
`transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/transactions.rb:95:in `transaction’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/transactions.rb:121:in  
`transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/transactions.rb:133:in `save!’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/associations/association\_proxy.rb:123:in  
`send' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/associations/association_proxy.rb:123:in `method\_missing’  
/home/victor/workspaces/grow/app/controllers/activities\_controller.rb:76:in  
`update_user_balance' /home/victor/workspaces/grow/app/controllers/activities_controller.rb:33:in `new’

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/locking/optimistic.rb:64:in  
`update_without_callbacks' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/callbacks.rb:267:in `update\_without\_timestamps’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/timestamp.rb:39:in  
`update' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:1792:in `create\_or\_update\_without\_callbacks’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/callbacks.rb:242:in  
`create_or_update' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:1554:in `save\_without\_validation!’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/validations.rb:762:in  
`save_without_transactions!' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/transactions.rb:133:in `save!’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:59:in  
`transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/transactions.rb:95:in `transaction’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/transactions.rb:121:in  
`transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/transactions.rb:133:in `save!’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/associations/association\_proxy.rb:123:in  
`send' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/associations/association_proxy.rb:123:in `method\_missing’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/base.rb:1101:in  
`send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/base.rb:1101:in `perform\_action\_without\_filters’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/filters.rb:696:in  
`call_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/filters.rb:688:in `perform\_action\_without\_benchmark’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/benchmarking.rb:66:in  
`perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:in `measure’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/benchmarking.rb:66:in  
`perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/rescue.rb:83:in `perform\_action’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/base.rb:435:in  
`send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/base.rb:435:in `process\_without\_filters’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/filters.rb:684:in  
`process_without_session_management_support' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/session_management.rb:114:in `process’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/base.rb:334:in  
`process' /usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/dispatcher.rb:41:in `dispatch’  
/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/webrick\_server.rb:113:in  
`handle_dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/webrick_server.rb:79:in `service’  
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run’  
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread' /usr/lib/ruby/1.8/webrick/server.rb:162:in `start’  
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' /usr/lib/ruby/1.8/webrick/server.rb:95:in `start’  
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each' /usr/lib/ruby/1.8/webrick/server.rb:92:in `start’  
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start' /usr/lib/ruby/1.8/webrick/server.rb:82:in `start’  
/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/webrick\_server.rb:63:in  
`dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/commands/servers/webrick.rb:59 /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem\_original\_require’  
/usr/local/lib/site\_ruby/1.8/rubygems/custom\_require.rb:27:in `require' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/dependencies.rb:495:in `require’  
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active\_support/dependencies.rb:342:in  
`new_constants_in' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/dependencies.rb:495:in `require’  
/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/commands/server.rb:39  
/usr/local/lib/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require' /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’  
script/server:3

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/locking/optimistic.rb:64:in  
`update_without_callbacks' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/callbacks.rb:267:in `update\_without\_timestamps’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/timestamp.rb:39:in  
`update' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:1792:in `create\_or\_update\_without\_callbacks’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/callbacks.rb:242:in  
`create_or_update' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:1554:in `save\_without\_validation!’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/validations.rb:762:in  
`save_without_transactions!' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/transactions.rb:133:in `save!’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:59:in  
`transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/transactions.rb:95:in `transaction’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/transactions.rb:121:in  
`transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/transactions.rb:133:in `save!’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/associations/association\_proxy.rb:123:in  
`send' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/associations/association_proxy.rb:123:in `method\_missing’  
/home/victor/workspaces/grow/app/controllers/activities\_controller.rb:76:in  
`update_user_balance' /home/victor/workspaces/grow/app/controllers/activities_controller.rb:33:in `new’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/base.rb:1101:in  
`send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/base.rb:1101:in `perform\_action\_without\_filters’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/filters.rb:696:in  
`call_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/filters.rb:688:in `perform\_action\_without\_benchmark’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/benchmarking.rb:66:in  
`perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:in `measure’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/benchmarking.rb:66:in  
`perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/rescue.rb:83:in `perform\_action’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/base.rb:435:in  
`send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/base.rb:435:in `process\_without\_filters’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/filters.rb:684:in  
`process_without_session_management_support' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/session_management.rb:114:in `process’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action\_controller/base.rb:334:in  
`process' /usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/dispatcher.rb:41:in `dispatch’  
/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/webrick\_server.rb:113:in  
`handle_dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/webrick_server.rb:79:in `service’  
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run’  
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread' /usr/lib/ruby/1.8/webrick/server.rb:162:in `start’  
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' /usr/lib/ruby/1.8/webrick/server.rb:95:in `start’  
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each' /usr/lib/ruby/1.8/webrick/server.rb:92:in `start’  
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start' /usr/lib/ruby/1.8/webrick/server.rb:82:in `start’  
/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/webrick\_server.rb:63:in  
`dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/commands/servers/webrick.rb:59 /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem\_original\_require’  
/usr/local/lib/site\_ruby/1.8/rubygems/custom\_require.rb:27:in `require' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/dependencies.rb:495:in `require’  
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active\_support/dependencies.rb:342:in  
`new_constants_in' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/dependencies.rb:495:in `require’  
/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/commands/server.rb:39  
/usr/local/lib/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require' /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’  
script/server:3

Request

Parameters: {“commit”=\>“Save changes”, “transfer”=\>{“approval”=\>“true”},  
“activity”=\>{“size”=\>“1”, “transactionable\_type”=\>“Transfer”,  
“description”=\>“a”, “user\_id”=\>“3”}}

Show session dump

* * *

:current\_user\_id: 1  
flash: !map:ActionController::Flash::FlashHash {}

Ryan B. wrote:

> > I know see one of the major drawbacks of Ruby as a language: very poor  
> > error messages.
> 
> I’m going to have to disagree with you there. The error messages are  
> extremely detailed and the backtrace lets you see which files called  
> what  
> method on what line (unlike certain other languages).
> 
> Can you show us the complete method that you’re calling?
> 
> You could also try user.save! without the self.
> 
> –  
> Ryan B.  
> [http://www.frozenplague.net](http://www.frozenplague.net)  
> Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [January 4, 2008, 7:12am UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/9 "2008-01-04T07:12:52Z")

</div>

On Jan 3, 2008 8:52 PM, Victor Al [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Application Trace | Framework Trace | Full Trace
> 
> /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/locking/optimistic.rb:64:in  
> `update\_without\_callbacks’

And what is in this file?

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/locking/optimistic.rb:64:in

I’d bet it has something to do with lock\_version being nil.

–  
Rick O.

> **[Lighthouse - Beautifully Simple Issue Tracking](https://lighthouseapp.com/)**

[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)

> **[Mephisto—The best blogging system ever](http://mephistoblog.com)**

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [January 4, 2008, 7:22am UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/10 "2008-01-04T07:22:13Z")

</div>

There should be a much bigger backtrace than just that. Show us all of  
it  
please.

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [January 4, 2008, 7:29am UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/11 "2008-01-04T07:29:41Z")

</div>

What exactly would that mean and how would I fix it?  
If you want to see a larger stacktrace how would I get that?

Thanks.

Rick O. wrote:

> On Jan 3, 2008 8:52 PM, Victor Al [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Application Trace | Framework Trace | Full Trace
> > 
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/locking/optimistic.rb:64:in  
> > `update\_without\_callbacks’
> 
> And what is in this file?
> 
> /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/locking/optimistic.rb:64:in
> 
> I’d bet it has something to do with lock\_version being nil.
> 
> –  
> Rick O.  
> [http://lighthouseapp.com](http://lighthouseapp.com)  
> [http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
> [http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [January 4, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/12 "2008-01-04T20:30:24Z")

</div>

On Jan 4, 2008 1:29 AM, Victor Al [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/locking/optimistic.rb:64:in  
> > > `update\_without\_callbacks’
> > 
> > And what is in this file?
> > 
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/locking/optimistic.rb:64:in
> > 
> > I’d bet it has something to do with lock\_version being nil.

Victor,

First off, you really should learn not to top-post, adding your  
comments to the end rather than the beginning of messages (after  
appropriately trimming what’s being quoted, makes it much easier to  
follow the thread.

That said, let me expand on Rick’s sage nudge on what to look for.

Here are three lines from  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active\_record/locking/optimistic.rb  
starting with line 62, as the name of the file implies this has to do  
with ActiveRecord’s optimistic locking feature.

```
    lock_col = self.class.locking_column
    previous_value = send(lock_col)
    send(lock_col + '=', previous_value + 1)

```

Now what’s blowing up is that previous\_value + 1, and previous\_value  
is obviously nil from the error message.

Where did previous value come from? Well it came from an attribute in  
the AR object whose name came from the class method locking\_column,  
which unless you’ve overridden it, will return the name of the  
standard attribute name which AR uses for the lock field which is  
lock\_version.

Note that you posted this:

#\<User:0xb70ea1e8 @attributes={“salt”=\>“L8Qymoyol2O7rA==”,  
“updated\_at”=\>“2007-12-30 09:58:06”, “details”=\>“Valentin Chilaru, New  
York NY”, “lock\_version”=\>nil,  
…

Showing that lock\_version is indeed nil.

Okay, now that we know that, we need to look at how AR expects you to  
set up lock\_version. By convention it should be an integer field with  
a default value of 0, and this should be defined in a migration.

class FixLockVersionInUser \< ActiveRecord::Migration  
def self.up  
execute “UPDATE users SET lock\_version = 0 WHERE lock\_version  
IS NULL”  
change\_column :users, :lock\_version, :integer, :default =\> 0  
end

```
def self.down
    # You can probably just leave this out but to be pro-forma we

```

put things back the way they were on a down.  
change\_column :users, :lock\_version, :integer  
end  
end

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [January 4, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/13 "2008-01-04T22:01:23Z")

</div>

> That said, let me expand on Rick’s sage nudge on what to look for.

Thanks for expanding that, Rick 🙂

FWIW, Rails 2.0.x should fix this by calling #to\_i on the nil value  
first. I can’t remember exactly when the fix was applied, so it might  
be in the next release of rails. It’s definitely in edge.

–  
Rick O.

> **[Lighthouse - Beautifully Simple Issue Tracking](https://lighthouseapp.com/)**

[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)

> **[Mephisto—The best blogging system ever](http://mephistoblog.com)**

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [January 4, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/14 "2008-01-04T22:15:32Z")

</div>

On Jan 4, 2008 4:00 PM, Rick O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > That said, let me expand on Rick’s sage nudge on what to look for.
> 
> Thanks for expanding that, Rick 🙂

Hey, we Ricks need to hang together!

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![bluestar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bluestar](https://www.ruby-forum.com/u/bluestar)\
**Post date:** [January 4, 2008, 10:33pm UTC](https://www.ruby-forum.com/t/nil-error-on-activerecord-save/124585/15 "2008-01-04T22:33:05Z")

</div>

Rick Denatale wrote:

> On Jan 4, 2008 4:00 PM, Rick O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > That said, let me expand on Rick’s sage nudge on what to look for.
> > 
> > Thanks for expanding that, Rick 🙂
> 
> Hey, we Ricks need to hang together!
> 
> –  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

Guys, thanks a lot for helping me find the cause of this error! I now  
know to look more carefully at the error log. I had no idea this was due  
to the locking being nil, and I was going to mistakenly stop using  
polymorphic associations thinking it was due to it.
