Active Record Postgresql handling of Dates

D

ded

I am trying to convert one db to Active Record based objects, so I read
in the rows from the old db, create an AR object, then do o.save.

Both dbs are postgres. One of the fields is a date field.

When I read it from the old table with dbi, the system recognizes it as
a date field and contructs a Date object. But when I assign the object
to an attribute in the AR object, then do o.save, I get this error
message:

====================================
/usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:462:in
`log': ERROR: invalid input syntax for type date: "---
!ruby/object:DBI::Date (ActiveRecord::StatementInvalid)
day: 23
month: 4
year: 2004"
: INSERT INTO owners ("city", "filed_on", "name", "zip", "middle",
"street1", "country", "last", "street2", "cik", "first", "is_entity",
"eolid", "hon", "stamp", "state", "suffix") VALUES('Oakland', '---
!ruby/object:DBI::Date
day: 23
month: 4
year: 2004', 'BERRIE ANGELICA', '07436', '', '40 Russ Berrie & Co Inc',
'United States', 'Berrie', '111 Bauer Drive', 1249690, 'Angelica', 'f',
NULL, '', NULL, 'NJ', '') from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/connection_adapters/postgresql_adapter.rb:109:in
`execute'
===========================================

It's the filed_on field that is causing the problems and the only Date
field in the record.

Anyone know what the problem is? My suspicion is that the postgres
adapter for AR is not able to cope with postgres 'date' fields, which
is puzzling for such a fundemental data type.

I would appreciate any help. I am new to Ruby and, of course, active
record.

Thanks.
 
D

David Naseby

I am trying to convert one db to Active Record based objects, so I read
in the rows from the old db, create an AR object, then do o.save.
=20
Both dbs are postgres. One of the fields is a date field.
=20
When I read it from the old table with dbi, the system recognizes it as
a date field and contructs a Date object. But when I assign the object
to an attribute in the AR object, then do o.save, I get this error
message:

It constructs a DBI::Date object, not a Date object. Active Record has
no idea what a DBI::Date object is, nor how it relates to a date, so
it tries to save it by marshalling with YAML. I haven't used
DBI::Date, but if there is a #to_time method, use it before assigning
the attribute, otherwise you can do something like:
owner.filed_on =3D Time.local(dbi_record.filed_on.year,
dbi_record.filed_on.month, ... )

Again, not sure of the API, but you should get the idea.

--=20
David Naseby
http://homepages.ihug.com.au/~naseby/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top