2011-10-02 2 views
0

나는 STI 관계가있다. 여기서 Commentable은 수퍼 클래스이고 NewsComment는 서브 클래스이다. Commentable에서 내가 가진 : STI와 가상 속성 상속 (레일즈 2.3)

attr_accessor :opinionated 

def after_initialize 
    self.opinionated = true 
end 

그리고 NewsComment에서

:

attr_accessor :headliner 

def after_initialize 
    self.headliner = true 
end 

인스턴스화 NewsComment이 self.opinionated 버지니아 상속되지 않은 경우. 왜 그런가요? 그리고 어떻게 NewsComment가 Commentable에서 상속되도록 강제 할 수 있습니까?

답변

0

NewsComment 객체를 어떻게 인스턴스화합니까? after_initialize 콜백은 객체가 파인더에 의해 인스턴스화 될 때만 실행됩니다. 또한 메서드를 정의하는 방식에 따라 메서드가 재정의 될 수 있습니다. DSL 스타일 방법을 사용할 경우 : ...

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