ruby-on-railsrubyruby-on-rails-3multi-table-inheritance

Multiple Table Inheritance, columns not accessible from sub class


UPDATE:
changing table_name to self.table_name = seems to have convinced rails to use the correct table.

I am, however, now getting these weird errors

   Mysql2::Error: Unknown column 'templates.deleted_at' in 'where clause': SELECT `objekts`.* FROM `objekts`  WHERE (`templates`.`deleted_at` IS NULL)

ORIGINAL QUESTION:

I have a Template, and an Objekt:

class Template < ActiveRecord::Base
  def status; 0; end # Template doesn't have a status column, so define default
end

class Objekt < Template
  table_name = "objekts" # there is a status column in this table
end

but when I do, Objekt.new.attributes in the console, it only lists the attributes from the Template object, and doesn't list any of the Objekt.

Every column on the Template is also available in the Objekt, but the Objekt has an additional 10 columns (mostly flags)

what's going on here? why does rails not hook up the Objekt class to the objekts table?


Solution

  • Ref the comments above when I refer to composition over inheritance I was thinking about something like this...

    class Objekt < ActiveRecord::Base
      include Template
    
      def status
        self.random_column_name || super
      end
    end
    
    module Template
      def status
        0
      end
    end