2014-06-24 2 views
0

내 의견 모델에 적절한 멤버에게 전달되는 알림을 생성하는 콜백이 있는데, current_member가 자신의 주석 처리 가능한 객체에 댓글을 달고있는 경우 알림을 보내지 않도록하고 싶습니다. 내가 사용 해봤하지 않는 한이 같은 조건 :콜백에서 조건 사용하기 레일

after_create :create_notification, on: :create, unless: Proc.new { |commentable| commentable.member == current_member } 

def create_notification 
    subject = "#{member.user_name}" 
    body = "wrote you a <b>Comment</b> <p><i>#{content}</i></p>" 
    commentable.member.notify(subject, body, self) 
end 

하지만이 오류를 얻을 : undefined local variable or method 'current_member' for #<Comment:0x746e008

어떻게 내가 원하는처럼이 일을 어떻게해야합니까?

답변

3

current_user 또는 모델 레이어에서 그런 것들을 사용하려고하면 꽤 비정형적입니다. 한 가지 문제는 모델 레이어를 컨트롤러 레이어의 현재 상태에 실제로 연결하는 것이므로 모델 테스트가 훨씬 어렵고 오류가 발생하기 쉽습니다.

내가 권장하는 것은 after_create 후크를 사용하지 않고 대신 컨트롤러 레이어에 알림을 생성하는 것입니다. 이렇게하면 모든 농구를 뛰어 넘을 필요없이 current_user에 액세스 할 수 있습니다.

+0

올바른 방향으로 나를 가리켜 주셔서 감사합니다. 어쨌든 current_member를 호출 할 필요가 없었습니다. 단, 다음과 같은 경우는 예외입니다 : Proc.new {| comment | comment.member.id == comment.commentable.member.id}'대신에 – iamdhunt