Prev | Current Page 324 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

username == "Kevin"
rescue ActiveRecordError => e
puts "ActiveRecordError thrown: #{e}"
end
SubclassNotFound
This error is related to single-table inheritance and improper reference to a subclass from
within your data.
Within ActiveRecord, you can define a subclass that inherits from an ActiveRecord::Base
class. This class association is stored, within the database in the inherited Active Record superclass
table, in a field called type by default (the field you defined with the Base.inheritance_column
method overrides this). If, for some reason, you have data in this field that does not relate to
a defined subclass within your program, you will generate the SubclassNotFound error.
In the following example, we assume that we have the following data within our database:
# data stored in our database (represented here in CSV format to save space)
id,username,type
1,Kevin,Husband
2,Catherine,Wife
And now, when we attempt to work with the second record in our data within our
ActiveRecord application, we will actually raise a SubclassNotFound error, because we have no
defined Wife subclass of Account:
CHAPTER 6 ?–  ACTIVE RECORD TESTING AND DEBUGGING 145
# program that throws SubclassNotFound
require 'rubygems'
require_gem 'activerecord'
ActiveRecord::Base.


Pages:
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336