Skip to content

Commit fd3a7a2

Browse files
authored
Merge pull request rails#49774 from stevepolitodesign/sp-disable-inheritance
Improve STI documentation
2 parents b863551 + ef299a5 commit fd3a7a2

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 nil"
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 +nil+
152+
#
153+
# self.inheritance_column = nil
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
@@ -2904,6 +2904,46 @@ will run a query like:
29042904
SELECT "vehicles".* FROM "vehicles" WHERE "vehicles"."type" IN ('Car')
29052905
```
29062906

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

0 commit comments

Comments
 (0)