Accessing created_on causes type error


#1

I assume that this is some newbie stupidity, but I haven’t found my
way around this problem. I have an ActiveRecord object fetched
from the database, and I need to evaluate the contents of the
updated_on field. However, any attempt to access that data in the
program results in a TypeError with the message ‘no implicit conversion
from nil to integer’.

cust = Customer.find(:first, :conditions => [“custid = ?”, custid]
if cust.updated_on.nil?
…never gets here
end

Any help would be much appreciated

Ken

I use the words you taught me. If they don’t mean anything any more,
teach me others. Or let me be silent.
Samuel Beckett (Clov, Endgame)


#2

Kenneth D. wrote:

However, any attempt to access that data in the
program results in a TypeError with the message ‘no implicit conversion
from nil to integer’.

cust = Customer.find(:first, :conditions => [“custid = ?”, custid]
if cust.updated_on.nil?
…never gets here
end

Hi Kenneth,

Is cust nil? Is custid nil? Try running the code with script/console.

Dan


#3

Quoth Dan Perez (removed_email_address@domain.invalid):

Hi Kenneth,

Is cust nil? Is custid nil? Try running the code with script/console.

Dan

No, custid was not nil. FWIW, this is part of a support script which
does backend things for a rails project, so was not being run under
rails. As it turns out, the problem I have above only happens
when I am running the script in the debugger. It’s very strange.
cust is a fully initialized object. I can access values in the
debugger,
including the updated_on field. But when the next line of code tests
to see if updated_on is nil, all hell breaks loose.

Ken

I use the words you taught me. If they don’t mean anything any more,
teach me others. Or let me be silent.
Samuel Beckett (Clov, Endgame)


#4

Kenneth D. wrote:

end

  1. Are you sure that there is a customer found?
  2. If there is a customer found, what does the database say its
    updated_on value should be?
  3. What db are you using, and what is the type of updated_on in the db?
  4. What’s the stack trace?

Ray


#5

No, custid was not nil. FWIW, this is part of a support script which
does backend things for a rails project, so was not being run under
rails. As it turns out, the problem I have above only happens
when I am running the script in the debugger. It’s very strange.
cust is a fully initialized object. I can access values in the
debugger,
including the updated_on field. But when the next line of code tests
to see if updated_on is nil, all hell breaks loose.

Weird. I assume you’re not missing the ‘)’ in your actual code, just
the post, right?

Dan


#6

Quoth Ray B. (removed_email_address@domain.invalid):

if cust.updated_on.nil?
…never gets here
end

  1. Are you sure that there is a customer found?

Yes.

  1. If there is a customer found, what does the database say its
    updated_on value should be?

‘0000-00-00 00:00:00’. The cust object agrees, if I look at it in the
debugger.

  1. What db are you using, and what is the type of updated_on in the db?

MySQL 5.0.17
updated_on timestamp NOT NULL default ‘0000-00-00 00:00:00’

  1. What’s the stack trace?

Here’s debugger output including the stack trace:
/usr/local/adm/accnt/script/customer.rb:112: webcust =
WebCustomer.find(:first, :conditions => [“custid = ?”, self.custid])
(rdb:1) n
/usr/local/adm/accnt/script/customer.rb:113: if webcust.nil?
(rdb:1) n
/usr/local/adm/accnt/script/customer.rb:113: if webcust.nil?
(rdb:1) n
/usr/local/adm/accnt/script/customer.rb:131: if not
webcust.updated_on.nil?
(rdb:1) n
/usr/local/adm/accnt/script/customer.rb:131: if not
webcust.updated_on.nil?
(rdb:1)
n
/pkg/ruby-1.8.2/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/abstract/schema_definitions.rb:112:
no implicit conversion from nil to integer' (TypeError) from /pkg/ruby-1.8.2/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/mysql_adapter.rb:278:ineach’
from
/pkg/ruby-1.8.2/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/mysql_adapter.rb:278:in
columns' from /pkg/ruby-1.8.2/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:734:incolumns’
from
/pkg/ruby-1.8.2/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:742:in
columns_hash' from /pkg/ruby-1.8.2/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1532:indefine_read_methods’
from
/pkg/ruby-1.8.2/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1482:in
method_missing' from /usr/local/adm/accnt/script/customer.rb:131:insave_to_sql’
from customer_upload.rb:17
from customer_upload.rb:13:in `each_line’
from customer_upload.rb:13
/pkg/ruby-1.8.2/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/abstract/schema_definitions.rb:112:
Time.send(Base.default_timezone, *time_array) rescue nil

The code runs fine outside of the debugger.

Ken

I use the words you taught me. If they don’t mean anything any more,
teach me others. Or let me be silent.
Samuel Beckett (Clov, Endgame)