Enums and STI

624 views
Skip to first unread message

Zac Moody

unread,
May 8, 2014, 11:00:02 AM5/8/14
to rubyonra...@googlegroups.com
Hi all,

I was thinking that someone might want the type column used with Single-Table Inheritance to be an Enum rather than a string.  Is the counter argument that STI is responsible for the type column so it doesn't matter?

If the inheritance_column did support enum, would the resulting code be repeating itself too much?  

class User < ActiveRecord::Base
  enum type: [:student, :teacher]
end

class Teacher < User
end
class Student < User
end

If there's interest in this feature I would be happy to work on adding it.

Zac

Tomas Varneckas

unread,
May 8, 2014, 12:25:20 PM5/8/14
to rubyonra...@googlegroups.com
Hi,

We have a project at my workplace which heavily relies on a custom in-house extension to ActiveRecord which does this
(but ofcourse in an uglier way). It'd be great if this was a builtin feature.

Hardcoding class names in table rows makes for tedious migrations whenever classes are renamed.

Good luck! :)


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Zac Moody

unread,
May 13, 2014, 10:37:42 AM5/13/14
to rubyonra...@googlegroups.com
Of course, if this was implemented then polymorphic association `_type` fields should also support Enums for consistency.  I think this would be advantageous for the same reasons.

Matt Jones

unread,
May 13, 2014, 10:57:28 AM5/13/14
to rubyonra...@googlegroups.com
On May 8, 2014, at 12:25 PM, Tomas Varneckas <t.var...@gmail.com> wrote:

Hi,

We have a project at my workplace which heavily relies on a custom in-house extension to ActiveRecord which does this
(but ofcourse in an uglier way). It'd be great if this was a builtin feature.

Hardcoding class names in table rows makes for tedious migrations whenever classes are renamed.


I’m not sure that a call to `update_all` really counts as “tedious”:

StiBaseClass.where(type: ‘OldType’).update_all(type: ‘NewType’)

The biggest issue I see with using enums for this is that the possible values of an enum need to be listed *someplace* - reflecting on all the subclasses of StiBaseClass won’t work since the order isn’t stable under renaming.

This applies doubly to using enums in polymorphic association _type fields…

—Matt Jones
signature.asc
Reply all
Reply to author
Forward
0 new messages