views:

334

answers:

2

Hello,

I have a class that derives from ActiveRecord::Base. This class has a subclass that is differentiated in the database by a type id column. How do I make sure that all the inherited active record functions in the derived classes append the type_id column to the conditions hash on any queries that are executed by the derived class? Are there any kind of "before" functions that I could override to inject a condition into the process for the derived classes?

Thanks!

+3  A: 

If your column was called "type", ActiveRecord would do this automatically for you.

Ryan Bigg
Thanks - this works great when I changed to a column called type. ActiveRecord puts the name of the model into the type column for each object.
Mattew
If you don't wish to change the column name then you can also use the set_inheritance_column declaration
Steve Weet
+1  A: 

What you are doing is called Single Table Inheritance (STI). That's why what Radar said about your column being called type works.

srboisvert