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.
9e6f139fdf4818d7999288c3f8e1ac4e?d=identicon&s=25 Kenneth Dunlap (Guest)
on 2006-04-26 23: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)
49c38b11918618294f164df6cd9d84c1?d=identicon&s=25 Dan Perez (perezda)
on 2006-04-27 01:28
Kenneth Dunlap 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
9e6f139fdf4818d7999288c3f8e1ac4e?d=identicon&s=25 Kenneth Dunlap (Guest)
on 2006-04-27 18:10
(Received via mailing list)
Quoth Dan Perez (perezda@yahoo.com):
> >
>
> 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)
49c38b11918618294f164df6cd9d84c1?d=identicon&s=25 Dan Perez (perezda)
on 2006-04-27 18: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
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-04-27 22:15
(Received via mailing list)
Kenneth Dunlap 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
9e6f139fdf4818d7999288c3f8e1ac4e?d=identicon&s=25 Kenneth Dunlap (Guest)
on 2006-05-03 18:58
(Received via mailing list)
Quoth Ray Baxter (ray@warmroom.com):
> >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.