Postgres Arrays?

I would like to use the postgres array type from rails. I want be sure that I understand the current state of rails/postgres array compatibility.

While googling, I found Ticket #4664, "PostgreSQL array data types being returned as Fixnum". If I understand the ticket correctly, if I have a pre-existing database that uses, say, the postgres int, it will become a string in rails. But it is not clear whether I could update the record, or create a new record. I'm also not sure whether the patch works for floats and doubles as well.

I'm not familiar (yet?) with the rails code or the C postgres/rails adaptor. Hypothetically speaking, if someone wanted to "fix" this, would they need to look at rails code, the postgres adaptor, or both?

The rails postgres adapter probably.

FYI, I saw a very interesting Postgres patch hit Trac the other day. Really beeds up support.

http://dev.rubyonrails.org/ticket/8049

Hey Rick,

While googling, I found Ticket #4664, "PostgreSQL array data types being returned as Fixnum". If I understand the ticket correctly, if I have a pre-existing database that uses, say, the postgres int, it will become a string in rails. But it is not clear whether I could update the record, or create a new record. I'm also not sure whether the patch works for floats and doubles as well.

Currently the PostgreSQL adapter converts any array of any type to string format. You can read and write arrays as per the PostgreSQL documentation at PostgreSQL: Documentation: 16: 8.15. Arrays.

I'm not familiar (yet?) with the rails code or the C postgres/rails adaptor. Hypothetically speaking, if someone wanted to "fix" this, would they need to look at rails code, the postgres adaptor, or both?

Exactly what would you like to "fix"? When I was busy with the patch that Hunter kindly referenced I was thinking to maybe convert PostgreSQL arrays to true Ruby arrays, and vice versa. I decided not to because:

- it would add quite a lot of code complexity; - it would potentially break a lot of PostgreSQL-dependent applications out there that expect the string behavior; - it wouldn't make sense to have PostgreSQL work with Ruby arrays throughout Rails when the other database adapters don't.

Interested to hear your thoughts about improved handling of arrays.

- Roderick

Roderick,

Thanks for the info. You are right, if I can read/write postgres arrays and have them be strings in ruby, that is probably good enough.

I guess that I would need to create the table with an array outside of ruby?

- it wouldn't make sense to have PostgreSQL work with Ruby arrays throughout Rails when the other database adapters don't.

Well, this isn't the right place for a database independent/database specific debate. As a new rails user, I get the impression that database independence is highly valued in the rails community. Personally, I could live with some database dependence, but I'm hardly in a position to suggest any changes.