Reputation: 18530
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
A
for the type
column. this works but only solves a part of the problemA
model. the problem with this approach is that it filters out all B
rows for both modelsQuestions
B < A
in rails, where none of the models is abstract?Upvotes: 1
Views: 1408
Reputation: 160170
The easiest solution might be to create a new base class and have both A
and B
extend it.
The base class would set its table name to point to your current A
table (as
):
class NewBaseClass < ActiveRecord::Base
self.table_name = `as`
end
class A < NewBaseClass
# ...
end
class B < NewBaseClass
# ...
end
Upvotes: 3
Reputation: 23344
Try:
class A< ActiveRecord::Base
self.abstract = false
end
Another approach:
I would create another model called C
that would be self contained. It would include table that would have all of the attributes. And finally models A
and B
would have a polymorphic has_one relation to the C
model.
Upvotes: 0