开发者

Rails 3 ActiveRecord abstract objects

I'm trying to instantiate an object of abstract AR class开发者_StackOverflow中文版 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜