Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 933
Adrian Pacala Avatar asked Feb 17 '11 11:02

Adrian Pacala


1 Answers

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.

like image 170
idlefingers Avatar answered Oct 13 '22 12:10

idlefingers