[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Noob: DateTime /DBI/ActiveRecord possible error...who knows

Tom Willis

3/21/2005 11:57:00 PM

OK, I feel I've done the due dilligence (googledilligence) and I can
not figure this out.

I'm using ActiveRecord to hit mysql.

ddl: the tables relevant parts in question...
================================
# Table: 'transactions'
CREATE TABLE `pf_transactions` (
#...
`entry_date` datetime NOT NULL,
`tran_date` datetime NOT NULL,
#...
) TYPE=InnoDB;

The unit test code...
======================

entry_date = DateTime.new(2005,4,1)
tran_date = DateTime.new(2005,4,1)

tran = PFTransaction.new("entry_date"=>entry_date,"tran_date"=>tran_date)
tran.save

assert_not_nil tran.id #works

t2 = PFTransaction.find(tran.id) #works
assert_not_nil t2 #works
assert_equal t2.id,tran.id, "id" #works
assert_equal t2.entry_date,tran.entry_date, "entry_date" #error
assert_equal t2.tran_date,tran.tran_date,"tran_date" #error

output
================================
Loaded suite test_transactions
Started
E
Finished in 2.396059 seconds.

1) Error:
test_transaction(TC_TestTransaction):
NoMethodError: private method `gsub!' called for #<DateTime:
4906923/2,0,2299161>
/usr/lib/ruby/1.8/date/format.rb:253:in `_parse'
/usr/lib/ruby/1.8/parsedate.rb:9:in `parsedate'
/usr/local/lib/site_ruby/1.8/active_record/connection_adapters/abstract_adapter.rb:216:in
`string_to_time'
/usr/local/lib/site_ruby/1.8/active_record/connection_adapters/abstract_adapter.rb:184:in
`type_cast'
/usr/local/lib/site_ruby/1.8/active_record/base.rb:1151:in `read_attribute'
/usr/local/lib/site_ruby/1.8/active_record/base.rb:1128:in `method_missing'
test_transactions.rb:65:in `test_transaction'

1 tests, 3 assertions, 0 failures, 1 errors
So I guess my question is, is there a trick to getting DateTime values
into out of Mysql through ruby dbi, and active_record?

I didn't get any hits that indicated this was a known bug and there
was a workaround, so I'm assuming I'm doing something incorrectly.
Here's to hoping.
--
Thomas G. Willis
http://paperbac...


1 Answer

Tom Willis

3/23/2005 12:48:00 AM

0

On Mon, 21 Mar 2005 18:56:26 -0500, Tom Willis <tom.willis@gmail.com> wrote:
> OK, I feel I've done the due dilligence (googledilligence) and I can
> not figure this out.
>
> I'm using ActiveRecord to hit mysql.
>
> ddl: the tables relevant parts in question...
> ================================
> # Table: 'transactions'
> CREATE TABLE `pf_transactions` (
> #...
> `entry_date` datetime NOT NULL,
> `tran_date` datetime NOT NULL,
> #...
> ) TYPE=InnoDB;
>
> The unit test code...
> ======================
>
> entry_date = DateTime.new(2005,4,1)
> tran_date = DateTime.new(2005,4,1)
>
> tran = PFTransaction.new("entry_date"=>entry_date,"tran_date"=>tran_date)
> tran.save
>
> assert_not_nil tran.id #works
>
> t2 = PFTransaction.find(tran.id) #works
> assert_not_nil t2 #works
> assert_equal t2.id,tran.id, "id" #works
> assert_equal t2.entry_date,tran.entry_date, "entry_date" #error
> assert_equal t2.tran_date,tran.tran_date,"tran_date" #error
>
> output
> ================================
> Loaded suite test_transactions
> Started
> E
> Finished in 2.396059 seconds.
>
> 1) Error:
> test_transaction(TC_TestTransaction):
> NoMethodError: private method `gsub!' called for #<DateTime:
> 4906923/2,0,2299161>
> /usr/lib/ruby/1.8/date/format.rb:253:in `_parse'
> /usr/lib/ruby/1.8/parsedate.rb:9:in `parsedate'
> /usr/local/lib/site_ruby/1.8/active_record/connection_adapters/abstract_adapter.rb:216:in
> `string_to_time'
> /usr/local/lib/site_ruby/1.8/active_record/connection_adapters/abstract_adapter.rb:184:in
> `type_cast'
> /usr/local/lib/site_ruby/1.8/active_record/base.rb:1151:in `read_attribute'
> /usr/local/lib/site_ruby/1.8/active_record/base.rb:1128:in `method_missing'
> test_transactions.rb:65:in `test_transaction'
>
> 1 tests, 3 assertions, 0 failures, 1 errors
> So I guess my question is, is there a trick to getting DateTime values
> into out of Mysql through ruby dbi, and active_record?
>
> I didn't get any hits that indicated this was a known bug and there
> was a workaround, so I'm assuming I'm doing something incorrectly.
> Here's to hoping.
> --
> Thomas G. Willis
> http://paperbac...
>
Man, amazing what you find when you can look at the source code and
you are not afraid. I should learn my llesson.


Anyway, Using a Time.local instead of DateTime.new works as expected,
however Time.gm somehow gets translated to local somewhere between the
db and retrieval for evaluation.

Oh well, I can work with this.



--
Thomas G. Willis
http://paperbac...