Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize Rails 3 STI column name

I have a class named Post:

class Post < ActiveRecord::Base
end

I have a class named Question that inheriting from Post:

class Question < Post
end

and I have a class named Answer that also inheriting from 'Post':

class Answer < Post
end

In Post, I have a column named post_type_id and its' type is Integer.
How do I use STI and specific column name & type to inherit from Post? 0 means Question and 1 means Answer. (0 & 1 is the value of post_type_id in posts table)

like image 597
VvDPzZ Avatar asked Feb 26 '12 16:02

VvDPzZ


1 Answers

There actually is a way, as with all things. You can override the find_sti_class method to look for your type based on an integer.

like image 134
Finbarr Avatar answered Oct 11 '22 21:10

Finbarr