Forum: Ruby on Rails accessing created_on causes type error

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.
Kenneth D. (Guest)
on 2006-04-27 01:28
(Received via mailing list)
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)
Dan P. (Guest)
on 2006-04-27 03:28
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
Kenneth D. (Guest)
on 2006-04-27 20:10
(Received via mailing list)
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)
Dan P. (Guest)
on 2006-04-27 20:38
> 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
Ray B. (Guest)
on 2006-04-28 00:15
(Received via mailing list)
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
Kenneth D. (Guest)
on 2006-05-03 20:58
(Received via mailing list)
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.

> 2) 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.


> 3) 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'

> 4) 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)
This topic is locked and can not be replied to.