Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Development Tools

Karianne Burns
Karianne Burns
541 Points

Single Table Inheritance, Account Entry Model: Hampton

Hi,

Hampton's screenshots move WAY too fast in the Single Table Inheritance video for Active Record Basics. Also sometimes he starts mumbling about stuff that's actually quite imp for new learners!

I couldn't follow what Hampton was saying and doing in the rails console just after we removed the :customer and :employee tables.

These are the commands in the rails console:

Employee.first Customer.first Account.first a = Account.first a.type = “Customer” a.save Customer.count

a = Account.find(2) a.type = “Employee” a.save Employee.first Employee.first.time_entries Employee.first.time_entries.all

t = TimeEntry.first t.employee_id = 2 t.save

Customer.first.time_entries

I skipped it because he was mumbling and went on to create the rails generate migration account_entries and account entry model. I edited my models like his.

Now in the rails console i get the error when I try to do Account.all:

2.0.0-p481 :001 > Account.first Account Load (0.2ms) SELECT accounts.* FROM accounts ORDER BY accounts.id ASC LIMIT 1 ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'Customer'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Account.inheritance_column to use another column for that information.

I think the error is because I missed the setting the "type" in the console before. I don't understand what is column "type" and how you set it. And how do we still have a Customer table or subclass or whatever when we dropped it when we created the account_entries db and account model? I need some clarification.

Just a suggestion, but some key bulletpoints for definitions of what the video talks about would be helpful, or an overview of the tasks accomplished in the lecture. I have to go back and pause every 15 sec to make sure I don't miss any commands or changes.

Kari

3 Answers

David Gross
David Gross
19,443 Points

I actually really like how Hampton did this screencast. I dont know if its perfect for beginners but he does explain everything in a cool way. What I would do is first watch the screen cast and make sure you understand it, if you had a problem understanding repeat it. Look for certain vocab your not familiar with and write them down. Sometimes I have had to watch and also read the transcripts multiple times to understand everything that is going on. If you are just typing the things out you hear. You are not really thinking of how to solve the problem.

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

  def down 
  end
end

In this project you want Customers and Employees to Inherit from the model Accounts. In this case we are use the table "type" to differentiate if the user is an employee or if they are a customer. That is also why in the single inheritance lesson Hampton dropped the tables customers and employees in the Remove_old_tables Migration. your remove_old_table migration should look like this

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

  def down 
  end
end

Make sure in your Employee and Customer models are inheriting to the Account model. Instead of ActiveRecord::Base. This allows for the single table inheritance to work.

class Employee < Account 
class Customer < Account

I have the same problem. I like Hampton, but he jumps around so much I can't follow what's going on. I've restarted this project twice already (watched the videos twice) and now have to start over again because I just can't get it to work (I get stuck at the RemoveOldTables step as it returns an error message that 'name' is an undefined method). Grrrr.... restart!

I'd like to throw my opinion into this discussion as well. I am also a beginner and am finding this video difficult to follow. As far as mindlessly following along and not thinking about what I am coding - that's not at all what I am doing. It takes me four to five times as long as a video's duration to complete a segment of the course since I stop, pause, rewind, etc. Unlike some folks, I believe it's possible to teach at a pace that's suitable for multiple levels (i.e. beginner to more advanced) without leaving some learners in the dust. Unfortunately it's not a skill everyone has. After this lesson, I may just decide to watch Hampton code on his own since too much of my code isn't doing what it's supposed to do and I'm better off finding another tutorial that explains things more clearly.