开发者

STI and virtual attribute inheritance (Rails 2.3)

Say I have an STI relationship, where Commentable is the super class, and NewsComment is the subclass. In Commentable I have:

attr_acce开发者_高级运维ssor :opinionated

def after_initialize
  self.opinionated = true
end

And in NewsComment:

attr_accessor :headliner

def after_initialize
  self.headliner = true
end

When instantiate NewsComment, the VA self.opinionated is not inherited. Why is that? And how can you "force" NewsComment to inherit from Commentable?


How are you instantiating the NewsComment object? The after_initialize callback is only executed when an object is instantiated by the finder. Also, the way you are defining the method may be overriding its behavior. What if you use the DSL style method?:...

class Commentable
  attr_accessor :opinionated

  after_initialize do
    self.opinionated = true
  end

end

class NewsComment < Commentable
  attr_accessor :headliner

  after_initialize do
    self.headliner = true
  end
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜