Rails Single Table Inheritance: How to Override a Value Written in a Type Field

On my system, I already defined STI. Doginherits from Animal, the table animalshas a column typewith a value "Dog".

Now I want to SpecialDoginherit from the dog, just slightly changed the behavior in some particular case. The data is all the same. I need all the queries that run through SpecialDogto return values ​​in a database with a type Dog.

My problem is that since I have a column type, rails adds WHERE "animals"."type" IN ('SpecialDog')to my query, so I cannot get the original records Dog. So I want to somehow override the value that rails uses when it accesses the database through SpecialDogto behave like that Dog.

Is there a way to override value rails for a type column?

+5
source share
1 answer

It seems like bad practice to do it this way, but below code should work as far as I can tell.

def self.sti_name
  "Dog"
end

My other suggestion is to not SpecialDoginherit from Dog, if possible.

+8
source

All Articles