Custom type support in ActiveRecord

I’m interested in using Postgres’s custom type support with Rails, particularly enumerated types. I see that this topic was previously brought up on this list here, but it did not seem to generate any discussion.

Would there be any interest in supporting Postgres custom enumerated types in ActiveRecord?

As a first pass, I would want to ability to run “CREATE TYPE foo AS ENUM(…)” statements in migrations and have them captured in schema.rb, as well as being able to use custom types alongside native types in my column definitions. I could eventually see it making sense to allow ActiveRecord “enum” declarations to defer to a native database enumeration if it exists, but the “enum” method doesn’t seem to have been aligned with the recent upgrades to the type system through the “attribute” keyword and ActiveRecord::Type.


I’m definitely interested in this.

If you just want to be able to create enums in an ActiveRecord::Migration, the SchemaPlus::Enums gem is worth a look.

I think the more interesting thing would to have ActiveRecord::Enumbacked by Postgres, but this raises hard questions about cross-database compatibility. It’s a bit of a can of worms!

There’s also an interesting post here that talks about some of the issues with the current implementation. I believe some of these have been addressed by Rails 5.

Thanks for the tip about SchemaPlus::Enums, Nicholas. That definitely makes creating the enums nicer.

Unfortunately, I don’t think that fixes the fact that a table that uses an enum as a column type can’t be dumped into schema.rb. It ends up hitting this code that calls “valid_type?(type)” on the connection adapter:

Since the PostgreSQL adapter currently only considers native types to be “valid”, tables using enums are not dumped:

I agree about the difficulties of having ActiveRecord::Enum use a native database enum, though I still think it would be nice. I’d settle for being able to do something with an “attribute” declaration instead and avoiding “enum”, since I don’t generally use the helper methods that “enum” generates.