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 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 -- Posted via http://www.ruby-forum.com/.
Quoth Dan Perez (perezda@yahoo.com):> 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 >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 -- Posted via http://www.ruby-forum.com/.
Kenneth Dunlap wrote:> 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 > end1) 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
Quoth Ray Baxter (ray@warmroom.com):> Kenneth Dunlap wrote: > > >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 > > 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)
Maybe Matching Threads
- SAS to R migration questions
- created_on, created_at defaulting to 2000/01/01 00:00:00
- AR many-many join tables - can they have created_on, updated_on ?
- created_on & updated_on - helper to display date only
- Created_on and updated_on in a non-ActiveRecord model