Search code examples
ruby-on-railssingle-table-inheritance

Factory Girl tries to access a table that does not exist when using Single Table Inheritance


Here is my scenario:

I want to store different types of events in my database. They all share the same fields but will behave differently. Therefore I used Single Table Inheritance.

The table definition for event looks like this:

class AddEvents < ActiveRecord::Migration
  def up
    create_table :events do |t|
      t.date :event_date
      t.string :type
      t.references :trackable , :polymorphic => true
    end
  end

  def down
    drop_table :events
  end
end

My event classes look like this (minimum working example):

class Event < ActiveRecord::Base

  self.abstract_class = true
  belongs_to :trackable, polymorphic: true

  [... some validation ] 
end

class View < Event; end
class Click < Event; end 
class Conversion < Event; end

The table gets created just fine. But when I try to call:

@view1 = FactoryGirl.build(:view1)

With :view1 defined like this:

factory :click1, class: Click do
  event_date TEST_CLICK_1_EVENT_DATE
end

I receive the following error message:

Failure/Error: @view1 = FactoryGirl.build(:view1) ActiveRecord::StatementInvalid: Mysql2::Error: Table 'adserve_test.views' doesn't exist: SHOW FULL FIELDS FROM 'views'

I also tried to create a click object from console which gives me the same error. Any ideas how to fix this?


Solution

  • The problem was self.abstract_class=true in the Event parent class. I totally forgot that an abstract class in rails is something completely different from languages like C# or Java. Or as the official website puts it:

    abstract_class
    Set this to true if this is an abstract class (see abstract_class?). If you are using inheritance with ActiveRecord and don't want child classes to utilize the implied STI table name of the parent class, this will need to be true.

    Which is 100% not what I wanted.