rails/rails — Issue #3325: STI should be able to handle a custom column name
Author: jfelchner
Created: 2011-10-13 23:41:34+00:00
State: open

Since STI is used primarily when you want to use inheritance and polymorphism, it stands to reason that in most cases you would want to namespace those classes. Here's an example:

# app/models/event.rb
class Event
has_one :gateway
end

# app/models/gateway/base.rb
module Gateway
class Base
# Base Gateway code here
end
end

# app/models/gateway/cool.rb
module Gateway
class Cool < Base
# Cool Gateway code here
end
end

# app/models/gateway/awesome.rb
module Gateway
class Awesome < Base
# Awesome Gateway code here
end
end

Now, if I'd named my gateway class `app/models/gateway.rb` and had a class name of `Gateway` it would "Just Work" but it's not very organized when you have a ton of files. I want all of my gateway code, not just the subclasses, inside the `app/models/gateway` directory.

It would be nice if, when using STI, the `:class_name` option respected STI so that it knew the base class was being specified. In this example, it would be:

# app/models/event.rb
class Event
has_one :gateway, :class_name => 'Gateway::Base'
end

The problem with this approach currently is that specifying the class name causes `SELECT ...... type = 'Gateway::Base'` to be inserted into all SQL finders which is not at all what we want in this case.