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

Binu Alexander
Binu Alexander
4,081 Points

Single table inheritance ?

JUST A NOTE :Hampton is way too fast and keeps changing his own statements , deleting the code he types on the screen !

HELP NEEDED !

COMMAND

irb(main):001:0> Account.all

ERROR

Account Load (0.4ms) SELECT accounts.* FROM accounts 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. from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:186:in rescue in find_sti_class' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:180:infind_sti_class' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:169:in discriminate_class_for_record' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/persistence.rb:67:ininstantiate' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/querying.rb:50:in block (2 levels) in find_by_sql' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/result.rb:51:inblock in each' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/result.rb:51:in each' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/result.rb:51:ineach' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/querying.rb:50:in map' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/querying.rb:50:inblock in find_by_sql' from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in instrument' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/querying.rb:49:infind_by_sql' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation.rb:638:in exec_queries' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation.rb:514:inload' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation.rb:243:in to_a' from /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation.rb:629:ininspect' ... 1 levels... from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in start' from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:inconsole' from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in run_command!' from /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands.rb:17:in<top (required)>' from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in require' from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:inblock in require' from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in load_dependency' from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:inrequire' from /Users/binualexander/OneDrive/My Documents/Programming/Ruby on Rails/biller/bin/rails:8:in <top (required)>' from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:inload' from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in block in load' from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:inload_dependency' from /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in load' from /usr/local/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from /usr/local/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require' from -e:1:in<main>'irb(main):002:0>

..."failed to locate the subclass: 'Customer'"... Looks like a relation issue. Can we see your Customer.rb code and your Account.rb code?

4 Answers

Ke Er Xiong
Ke Er Xiong
14,622 Points

I had the same issue and was able to fix it by removing @customer.time_entries and @customer.account_entries from customer.rb

With STI (single table inheritance) you will have a table, Accounts, in your db schema and also a Model in the Account.rb file.

In the table called "Accounts" you will need to add a field called "type". When you name the field "type", Rails knows that it is associated with STI and that you will have different types of accounts.

Did you add this field in a migration?m

gregory fenwick
gregory fenwick
7,569 Points

Hi Everyone,

I had the same issue after following the tutorial step by step, reviewing notes and writing all the code down to better analyse. For a learner this course is baptism by fire, however the overview of how everything fits together, relationships and just diving into the code does makes you understand or at least try to break it all logic down.

After googling I managed to find this solution, it seems as though this is a common issue.

class Account < ActiveRecord::Base has_many :account_entries

disable STI

self.inheritance_column = 'zoink'

end

Reference:

inheritance_column() public

Defines the name of the table column which will store the class name on single-table inheritance situations.

The default inheritance column name is type, which means it’s a reserved word inside Active Record. To be able to use single-table inheritance with another column name, or to use the column type in your own model for something else, you can set inheritance_column:

self.inheritance_column = 'zoink'

Thanks.

We don't have a time_entries table in our DB, we recently renamed it to account_entries.

So, comment the first line and all will be fine:

# @customer.time_entries # only TimeEntries
@customer.account_entries # a mix of TimeEntries and AccountEntries

Cheers!