Skip to content

Commit fcc000c

Browse files
Improve STI documentation
Add new sections to [STI Guides][] demonstrating how to override or disable the [inheritance_column][]. Also improve error message when `ActiveRecord::SubclassNotFound` is raised. [STI Guides]: https://guides.rubyonrails.org/association_basics.html#single-table-inheritance-sti [inheritance_column]: https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema.html#method-c-inheritance_column
1 parent 4c5c904 commit fcc000c

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

activerecord/lib/active_record/inheritance.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ def sti_class_for(type_name)
202202
"The single-table inheritance mechanism failed to locate the subclass: '#{type_name}'. " \
203203
"This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " \
204204
"Please rename this column if you didn't intend it to be used for storing the inheritance class " \
205-
"or overwrite #{name}.inheritance_column to use another column for that information."
205+
"or overwrite #{name}.inheritance_column to use another column for that information. " \
206+
"If you wish to disable single-table inheritance for #{name} set " \
207+
"#{name}.inheritance_column to :disabled."
206208
end
207209

208210
# Returns the value to be stored in the polymorphic type column for Polymorphic Associations.

activerecord/lib/active_record/model_schema.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ module ModelSchema
146146
# your own model for something else, you can set +inheritance_column+:
147147
#
148148
# self.inheritance_column = 'zoink'
149+
#
150+
# If you wish to disable single-table inheritance altogether you can set
151+
# +inheritance_column+ to +:disabled+
152+
#
153+
# self.inheritance_column = :disabled
149154

150155
##
151156
# :singleton-method: inheritance_column=

guides/source/association_basics.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,6 +2912,46 @@ will run a query like:
29122912
SELECT "vehicles".* FROM "vehicles" WHERE "vehicles"."type" IN ('Car')
29132913
```
29142914

2915+
### Overriding the inheritance column
2916+
2917+
There may be cases (like when working with a legacy database) where you need to
2918+
override the name of the inheritance column. This can be achieved with the
2919+
[inheritance_column][] method.
2920+
2921+
```ruby
2922+
# Schema: vehicles[ id, kind, created_at, updated_at ]
2923+
class Vehicle < ApplicationRecord
2924+
self.inheritance_column = "kind"
2925+
end
2926+
2927+
class Car < Vehicle
2928+
end
2929+
2930+
Car.create
2931+
# => #<Car kind: "Car">
2932+
```
2933+
2934+
### Disabling the inheritance column
2935+
2936+
There may be cases (like when working with a legacy database) where you need to
2937+
disable Single Table Inheritance altogether. Otherwise, you'll raise
2938+
[`ActiveRecord::SubclassNotFound`][].
2939+
2940+
This can be achieved by setting the [inheritance_column][] to `:disabled`.
2941+
2942+
```ruby
2943+
# Schema: vehicles[ id, type, created_at, updated_at ]
2944+
class Vehicle < ApplicationRecord
2945+
self.inheritance_column = :disabled
2946+
end
2947+
2948+
Vehicle.create!(type: "Car")
2949+
# => #<Vehicle type: "Car">
2950+
```
2951+
2952+
[inheritance_column]: https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema.html#method-c-inheritance_column
2953+
[`ActiveRecord::SubclassNotFound`]: https://api.rubyonrails.org/classes/ActiveRecord/SubclassNotFound.html
2954+
29152955
Delegated Types
29162956
----------------
29172957

0 commit comments

Comments
 (0)