Search code examples
ruby-on-railssti

Can this STI structure be achieved in Rails?


I want to have these models:

AbstractArticle 
Article < AbstractArticle
FeaturedArticle < AbstractArticle

I want the name of the database table to be articles and not abstract_articles. (As the convention suggests)

Is this possible with Rails? How can I tell my base model to use a different table? Will STI still work even though the table name does not follow the convention?


Solution

  • Yes, you can.

    class AbstractArticle <  ActiveRecord::Base
      set_table_name :articles
    end
    

    http://wiki.rubyonrails.org/rails/pages/howtouselegacyschemas#using_set_table_name_for_table_names_outside_the_rails_convention