2010-07-25 3 views
0

또 다른 question에 속성에 대한 질문이 있지만 연관성을 높이고 싶습니다. 레일스 ActiveRecord : 레코드가 특정 상태로 들어갈 때 특정 연결 잠그기

내가 간단한 블로깅 모델

class Discussion < ActiveRecord::Base 
    has_many :comments 
end 

class Comment < ActiveRecord::Base 
    belongs_to :discussion 
end 

토론은이 #closed있다 말해봐? 부울 상태. 나는 을 (를) 모두에 등록하고 있습니다. 닫힌 토론에 더 많은 의견을 남기십시오. 예를 들어,

discussion.comments << Comment.new 
discussion.comments.create(:text => 'Something') 

하고있을 수있는 다른 사람 ...

감사합니다,

- 제이슨

답변

0

를 추가해보십시오 검증 방법 모델 Comment에 :

class Comment < ActiveRecord::Base 
    belongs_to :discussion 

    def validate 
    errors.add_to_base("Discussion is closed!") if discussion.closed? && new_record? 
    end 
end 

이 유효성 검사는 토론에 새로운 설명을 추가하는 것을 방지하지만 이미 닫힌 토론에 대한 기존 의견을 편집 할 수 있습니다.

관련 문제