【问题标题】:Rails: implement auditing of table insertions/updatesRails:实现表插入/更新的审计
【发布时间】:2012-11-12 17:50:28
【问题描述】:

我是 ROR 的新手,正在从事货物跟踪项目。 这个想法是记录对发货位置的所有更改,以便当用户使用跟踪号跟踪他们的发货时,他们会看到当前位置和经过的位置历史记录。

我有一个 shipping 表和一个 shipping_locations 表。

  1. 发货

    +--------------------------+--------------+------+-----+---------+----------------+
    | Field                    | Type         | Null | Key | Default | Extra          |
    +--------------------------+--------------+------+-----+---------+----------------+
    | id                       | int(11)      | NO   | PRI | NULL    | auto_increment |
    | start_address_id         | int(11)      | YES  |     | NULL    |                |
    | end_address_id           | int(11)      | YES  |     | NULL    |                |
    | transportation_agency_id | int(11)      | YES  | MUL | NULL    |                |
    | customer_id              | int(11)      | NO   | MUL | NULL    |                |
    | status_id                | int(11)      | NO   | MUL | NULL    |                |
    | cargo_id                 | int(11)      | YES  | MUL | NULL    |                |
    | tracking_number          | varchar(255) | NO   | MUL | NULL    |                |
    | mode_of_transport_id     | int(11)      | YES  | MUL | NULL    |                |
    | expected_start_date      | date         | YES  |     | NULL    |                |
    | actual_start_date        | date         | YES  |     | NULL    |                |
    | expected_end_date        | date         | YES  |     | NULL    |                |
    | actual_end_date          | date         | YES  |     | NULL    |                |
    | created_at               | datetime     | NO   |     | NULL    |                |
    | updated_at               | datetime     | NO   |     | NULL    |                |
    | admin_user_id            | int(11)      | YES  | MUL | NULL    |                |
    +--------------------------+--------------+------+-----+---------+----------------+
    
  2. shipment_locations:(位置更新在此表上完成,位于 (current_location) 下)

    +------------------+--------------+------+-----+---------+----------------+
    | Field            | Type         | Null | Key | Default | Extra          |
    +------------------+--------------+------+-----+---------+----------------+
    | id               | int(11)      | NO   | PRI | NULL    | auto_increment |
    | shipment_id      | int(11)      | NO   | MUL | NULL    |                |
    | current_location | varchar(255) | YES  |     | NULL    |                |
    | final_location   | text         | YES  |     | NULL    |                |
    | created_at       | datetime     | NO   |     | NULL    |                |
    | updated_at       | datetime     | NO   |     | NULL    |                |
    +------------------+--------------+------+-----+---------+----------------+
    
  3. Shipment_history(建议)

    字段:

    • 身份证
    • shipment_id
    • 当前位置
    • created_at
    • updated_at

我想将 shipping_locations 表中的所有更新存储到这个历史表中,以便用户可以得到:

Time (Updated_at):----------------------------> Location:(current_location)
1/12/2012 11:30                                  222 John st.
1/12/2012 13:00                                  555 Paul st.
1/13/2012 07:30                                  final_location

如何通过控制器实现这一点,以便只有一个更新操作保存到历史表?

更新:

我能够使用此触发器获得货运跟踪日志,感谢 vladr

分隔符 $$

删除触发器shipment_after_update$$

shipments 更新后创建触发器shipment_after_update 每一行 开始 插入到 cargo_transit_histories (shipment_idcurrent_locationfinal_locationupdated_at) 价值观 (NEW.id, NEW.current_location, NEW.final_location, NOW()); 结束$$

分隔符;

【问题讨论】:

  • 您可能还需要考虑 (1) 使用数据库触发器,(2) 对表进行版本化 而不是使用单独的审计表
  • 谢谢 vladr 我会做一些关于使用触发器的研究,这看起来像是一种更清洁的方式。但是触发器应该有一个单独的表吧?
  • 有了触发器,你可以在触发器中做任何你想做的事情。另请查看stackoverflow.com/questions/5346367/…stackoverflow.com/questions/1697456/…
  • 感谢您的及时回复,将在尝试这些选项后进行报告。

标签: mysql ruby-on-rails logging transactions audit


【解决方案1】:

我不确定我是否正确,以及这是否是一个好方法,但我会尝试。 我建议你让 shipping_history 属于 shipping_locations。

因此,如果 shipping_history 具有 shipping_locations_id 您可以使用 after_update 回调来设置您需要的内容。

ShipmentLocations.model:

      #your other code
    after_update :track_changes_in_history

    def track_changes_in_history 
           @shipment_history=Shipment_history.where(:shipment_locations_id=>self.id).first_or_create!
@shipment_history.current_location = self.current_location
#so on
     end

但是在您的模型属性中,我看不到及时显示其状态的方法。为此,您可能最好单独设置例如 Map location:string 模型,它通过 has_many 声明属于所有其他模型,而不是您可以通过订购位置来显示它在历史中的状态,这属于这批货物。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    相关资源
    最近更新 更多