Hacker News new | ask | show | jobs
Rails 4.0 Sneak Peek: Expanded ActiveRecord Support for PostgreSQL Datatypes (reefpoints.dockyard.com)
59 points by phsr 5140 days ago
3 comments

I want support not just for PostgreSQL native datatypes, I want ActiveRecord support for adding my own custom datatypes.
You can pretty much do that right now (and have been able to for years) with composed_of: http://api.rubyonrails.org/classes/ActiveRecord/Aggregations... (search on that page for it)
The problem is that custom pg types are represented as strings in ActiveRecord < 4.0.

In AR 4.0, you can have ActiveRecord automatically cast them to a different type.

Type casting is fine but if you need to use schema.rb you'll have to use the sql alternative. Adding explicit support for the existing datatypes allows, amongst other things, the schema.rb
We had to abandon schema.rb early because we use PostGIS, functional indexes, partial indexes and foreign key constraints, none of which are supported. (FKs may be supported now, but at the time we had to monkeypatch AR to support it, before eventually abandoning schema.rb altogether.)

Schema.rb is inherently a broken concept anyway, because it's lossy. What you really want is the SQL, which is the only format that completely encapsulates your schema in AR. I often wish AR was declarative for that reason.

The default Rails tooling for loading and dumping the structure is also completely harebrained (google it and you will get a gazillion hits), so we wrote our own internal gem that does all of it for us, along with overriding db:test:prepare.

composed_of is the "other half" of what I want. I don't want to use custom ruby classes, I want to use custom Postgresql types.

Turns out support for that's in 4.0 too!

https://github.com/rails/rails/pull/4775

I'm curious to see how it works for types with multiple elements (composite types).
That's in ActiveRecord 4.0 as well!

https://github.com/rails/rails/pull/4775

Take a look at @tenderlove's comment down below about the actual implementation (as opposed to my crappy one in the issue body).

You can say what you want about rails, but i really admire the pace they make progress.
Rails is the new Java.