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)
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
Quoth Dan Perez ([email protected]):
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)
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
Quoth Ray B. ([email protected]):
if cust.updated_on.nil?
…never gets here
end
- Are you sure that there is a customer found?
Yes.
- 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.
- 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’
- 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:in
each’
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:in
columns’
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:in
define_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:in
save_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)