Hacker News new | ask | show | jobs
by jeremymcanally 5141 days ago
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)
2 comments

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).