Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby ActiveRecord Basics Migrations and Relationships Single Table Inheritance

Michael Hall
PLUS
Michael Hall
Courses Plus Student 30,909 Points

Can someone explain why we can't add the type during the migration.

class RemoveOldTables < ActiveRecord::Migration
  def up
    Customer.all.each do |c|
      Account.create(name: c.name, about: c.about, type: 'Customer')
    end
    Employee.all.each do |e|
      Account.create(name: e.name, email: e.email, type: 'Employee')
    end

    drop_table :customers
    drop_table :employees
  end

  def down
  end
end

When I run this I get the error

Invalid single-table inheritance type: Customer is not a subclass of Account

What does the error mean?

Oliver Duncan
Oliver Duncan
16,642 Points

Great question. I'd like to know as well.

2 Answers

Oliver Duncan
Oliver Duncan
16,642 Points

I think I might have the answer. If you first edit the models so that 'class Customer < Account', i.e. Customer inherits from Account and not from ActiveRecord::Base, it will allow your code to run. Same for employees, obviously.

Oliver Duncan
Oliver Duncan
16,642 Points

Of course, now I'm checking and it looks like none of the information from Customers or Employees was added to Accounts...whoops! I guess this is one of those things we'll just have to except sans explanation (for now, hopefully...)

Hamptin talks about this a bit later in the video. He should have done it during the migration, but didn't, and he fixes it eventually.