2015-02-24 128 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