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
精彩评论