2013-10-13 2 views
0

누군가가 올바른 방향으로 나를 가리킬 수 있기를 바랍니다.DataMapper : 레코드를 업데이트하기 전에 변경된 속성 만 가져 오기

update 메서드를 호출하고 값을 비교하기 전에 DataMapper로 새로운 속성 값과 이전 속성 값을 모두 보거나 가져 오는 방법이 있는지 알고 싶습니다.

시나리오는 다음과 같습니다. 티켓 리소스가 있으며 티켓에 대한 변경 사항을 다양한 이해 관계자에게 알릴 필요가 있습니다. 이메일 알림 때 지불 상태 변경, SMS 알림 때 티켓 GET의

현재 내 티켓 클래스 내에서,이 같은 콜백/필터 설정 한 등 지원 인력에 할당 :

before :update, :notify_changes 

def notify_changes 
    ticket = Ticket.get(self.id) # Get the original 
    if ticket.status != self.status 
     # Send out the email notification 
    end 
    if ticket.assigned_support != self.assigned_support 
     # Send out the SMS notification 
    end 
    # ... etc 
end 

ticket = Ticket.get(self.id)에서 데이터베이스를 다시 방문하지 않고도이 작업을 수행 할 수있는 더 효과적인 방법이 있습니까?

+0

당신이 업데이트 콜백 전에 싫어합니까? 또는'if ticket.status! = self.status'? – AdamT

+0

'ticket = Ticket.get (self.id)'에서 데이터베이스를 다시 치지 않고이 작업을 수행 할 수있는 방법을 찾고 싶습니다. 객체가 변경된 것을 알고 있으면 변경된 사항을 알 수 있기를 바랍니다. @AdamT – SeanNieuwoudt

답변

1

좋아, 나 자신을 알아 냈어.

before :update, :notify_changes 

def notify_changes 
    # The status property has been changed 
    if !dirty_attributes[Ticket.properties[:status]].nil? 
     # old status: original_attributes[Ticket.properties[:status]] 
    end   

    # The assigned_support property has been changed 
    if !dirty_attributes[Ticket.properties[:assigned_support]].nil? 
     # old status: original_attributes[Ticket.properties[:assigned_support]] 
    end   
end 

영감 참조 : This thread

1

예,이 더러운 언급되었다 내가 그걸 물었을 때 다른 사람이 자신이 같은 질문을 발견하면 여기 참조 용입니다. 조금만 더 추가하면 다른 사람이이 질문을 접하게됩니다.

속성 또는 모델 객체의 상태를 확인하기 위해 호출 할 수있는 몇 가지 메소드가 있습니다.

- (Boolean) attribute_dirty?(name) 
- (Boolean) clean? 
- (Boolean) dirty? 
- (Hash) dirty_attributes # your choice 
- (Hash) original_attributes 

DataMapper::Resource의 일부이며 여기에서 찾을 수 있습니다 : http://rubydoc.info/github/datamapper/dm-core/master/DataMapper/Resource