Adrian Pacala
Adrian Pacala

Reputation: 1011

Rails 3 ActiveRecord abstract objects

I'm trying to instantiate an object of abstract AR class for testing purposes. The model's defined like this:

class Basic < ActiveRecord::Base
  self.abstract_class = true

  def stuff
    raise NotImplementedError
  end
end

When I try to Basic.new, I get:

"Mysql2::Error: Table 'project_development.basics' doesn't exist"

Is it normal behavior? Do abstract AR classes are not intended to be instantiated even without (obviously impossible) persistence?

Using 1.9.2-p136 with Rails 3.0.4 / Mysql2 0.2.6

Edit:

It turns out that the error is caused by column definitions, which in the case of an abstract model cannot be fetched from the database.

class Basic < ActiveRecord::Base
  self.abstract_class = true
  @columns = []
end

Works like a charm.

Upvotes: 17

Views: 10883

Answers (1)

idlefingers
idlefingers

Reputation: 32047

This is normal behaviour. Abstract classes are not supposed to be instantiated. You should test the classes which inherit from the abstract class, not the abstract class itself.

Upvotes: 20

Related Questions