2015-02-24 81 views
0

的推薦做法給定具有屬性private_status:stringprivate_status_history:json(我正在使用Postgresql的json)的模型Orderstatus。我想記錄每次狀態轉換,以及進行更改的用戶。將當前用戶傳遞給模型

理想情況下是這樣的:

class Orderstatus < ActiveRecord::Base 
    after_save :track_changes 

    def track_changes 
    changes = self.changes 
    if self.private_status_changed? 
     self.private_status_history_will_change! 
     self.private_status_history.append({ 
           type: changes[:private_status], 
           user: current_user.id 
           })     
    end 
    end 
end 


class OrderstatusController <ApplicationController 
    def update 
    if @status.update_attributes(white_params) 
     # Good response 
    else 
     # Bad response 
    end 
    end 
end 

#Desired behaviour (process not run with console) 
status = Orderstatus.new(private_status:'one') 
status.private_status #=> 'one' 
status.private_status_history #=> [] 
status.update_attributes({:private_status=>'two'}) #=>true 
status.private_status #=> 'two' 
status.private_status_history #=> [{type:['one','two'],user:32] 

什麼是推薦的做法,以實現這一目標?除了通常使用Thread的一個。或者,也許,任何建議來重構應用程序的結構?

+0

在一個不相關的說明中,是否有一個原因是您手動執行此操作,而不是使用類似[紙張路徑](https://github.com/airblade/paper_trail)的東西? – mcfinnigan 2015-02-24 13:51:27

+0

謝謝你的提示。說實話,紙上談兵肯定會適合,但是因爲現在這是我需要跟蹤的唯一屬性;我寧願做手工,以避免添加一個新的寶石到堆棧。此外,我是關於實現這一目標的有趣方法:) – lllllll 2015-02-24 14:23:13

回答

0

於是,我終於塵埃落定此選項(我希望這不是驚人的任何人:S)

注: - 我通過一個實例通過從控制器到模型屬性modifying_userOrderstatus。該屬性是沒有保存到數據庫。 - 更改方法以將新更改附加到歷史記錄字段。即attr_will_change! + saveupdate_column + append

相關問題