Background
I have a rails 3 app that has a model named A
with the correspondent table as
. Now I need a new model B
that works exactly like A
but has some other associations and methods (besides those in A
).
Problem
I decided to use STI (Single Table Inheritance). I know I could do something like A < BaseModel
and B < BaseModel
, but there's already a lot of code assuming a table named as
and it would require too much work.
So I'm trying to do B < A
, where A
is not abstract. I added the type
column to as
. Now the model B
works perfect, but the model A
doesn't know anything about inheritance and completely ignores the type
column, so when I do A.create!
it says that the column type
cannot be empty. Also A.all
returns B
rows too.
What I've tried
- Using the default value
A
for thetype
column. this works but only solves a part of the problem - Using a default scope in the
A
model. the problem with this approach is that it filters out allB
rows for both models
Questions
- How can I accomplish
B < A
in rails, where none of the models is abstract? - Are there any other nice alternatives?
set_table_name
. A and B will extend this new base class. I don't know if it'll work, it's just what I thought of. – Dave Newtonset_table_name
. I'll try that and let you know. Thanks! – alfset_table_name
is deprecated.self.table_name=
is the new thing. If you post this as an answer I'll accept it. Thanks again! – alf