Re: "RuntimeError: Called id for nil" when saving

Jamis Buck
Fri, 19 Aug 2005 21:30:25 -0000

On Aug 19, 2005, at 3:23 PM, Eric Goodwin wrote:

> Thanks Jamis,
> I should have seem that there. In the console I wasn''t setting my
> User.current_user.id that I use. I''m getting a different error now
> though.
>
> >> asset = Assetstatus.find(1)
> => #<Assetstatus:0x39d6b08
@attributes={"title"=>"Not Started",
> "id"=>"1", "deleted"=>nil}>
> >> asset.save
> => true
> >> section = Section.find(1)
> => #<Section:0x39bffa8
@attributes={"updated_at"=>"2005-08-02
> 11:09:09", "title"=>"Activities", "c
> reated_by"=>"9", "lock_version"=>nil,
"id"=>"1", "updated_by"=>"9",
> "version"=>nil, "sectionstatus
> _id"=>"4", "deleted"=>"0",
"sectiontype_id"=>"1",
> "parent_id"=>"00000000000",
"position"=>"1", "cr
> eated_at"=>"2005-08-02 11:08:00"}>
> >> section.title = "Activities"
> => "Activities"
> >> section.save
> NoMethodError: WARNING:  You have a nil object when you probably
> didn''t expect it!  Odds are you
> want an instance of Array instead.
>
> Look in the callstack to see where you''re working with an object
> that could be nil.
> Investigate your methods and make sure the object is what you expect!

If you are using optimistic locking, you need to make sure your
lock_version column defaults to 0. Currently, it looks like it
defaults to NULL, so that column is nil, which is causing the error
when (I suspect) the locking module tries to increment it.

That''s my theory, anyway. :)

- Jamis

>
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> active_record/locking.rb:33:in
> `update_without_callbacks''
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> active_record/callbacks.rb:274
> :in `update_without_timestamps''
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> active_record/timestamp.rb:39:
> in `update_without_user''
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> usermonitor.rb:25:in `update''
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> active_record/base.rb:1139:in
> `create_or_update_without_callbacks''
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> active_record/callbacks.rb:249
> :in `create_or_update''
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> active_record/base.rb:965:in `
> save_without_validation''
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> active_record/validations.rb:6
> 50:in `save_without_transactions''
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> active_record/transactions.rb:
> 128:in `save''
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> active_record/transactions.rb:
> 128:in `transaction''
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> active_record/transactions.rb:
> 93:in `transaction''
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> active_record/transactions.rb:
> 120:in `transaction''
>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/
> active_record/transactions.rb:
> 128:in `save''
>        from (irb):14
>
> Any ideas,
> Thanks in advance.
> Eric
>
> Jamis Buck wrote:
>
>
>> On Aug 19, 2005, at 2:45 PM, Eric Goodwin wrote:
>>
>>
>>> Hey,
>>> I''m having an error when I try to save anything. Here is
the
>>> output  from the console. Anyone know why this would be happeing.
>>>
>>> >> asset = Assetstatus.find(1)
>>> => #<Assetstatus:0x3bd3bf0
@attributes={"title"=>"Not Started",
>>> "id"=>"1", "deleted"=>nil}>
>>> >> asset.deleted = 0
>>> => 0
>>> >> asset.save
>>> RuntimeError: Called id for nil, which would mistakenly be 4 --
>>> if  you really wanted the id of nil
>>> , use object_id
>>>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/
>>> lib/ usermonitor.rb:23:in `update''
>>>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/
>>> lib/ active_record/base.rb:1139:in
>>> `create_or_update_without_callbacks''
>>>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/
>>> lib/ active_record/callbacks.rb:249
>>> :in `create_or_update''
>>>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/
>>> lib/ active_record/base.rb:965:in `
>>> save_without_validation''
>>>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/
>>> lib/ active_record/validations.rb:6
>>> 50:in `save_without_transactions''
>>>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/
>>> lib/ active_record/transactions.rb:
>>> 128:in `save''
>>>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/
>>> lib/ active_record/transactions.rb:
>>> 128:in `transaction''
>>>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/
>>> lib/ active_record/transactions.rb:
>>> 93:in `transaction''
>>>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/
>>> lib/ active_record/transactions.rb:
>>> 120:in `transaction''
>>>        from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/
>>> lib/ active_record/transactions.rb:
>>> 128:in `save''
>>>        from (irb):13
>>>        from :0
>>>
>>
>>
>> I''m not sure what ''usermonitor.rb'' is... it
looks like it is in
>> the  activerecord directory, but I don''t believe it''s
a part of
>> ActiveRecord itself. Is it something you added, perchance? What
>> does  line 23 of that file look like?
>>
>> - Jamis