3

我对 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. ship_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(建议)

    领域:

    • ID
    • 装运编号
    • 当前位置
    • created_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$$

CREATE TRIGGER shipment_after_updateAFTER AFTER UPDATE on shipments FOR EACH BEGIN INSERT INTO cargo_transit_histories ( shipment_id, current_location, final_location, updated_at) 值 (NEW.id, NEW.current_location, NEW.final_location, NOW()); 结束$$

分隔符;

4

1 回答 1

0

我不确定我是否正确,如果这是一个好方法,但我会尝试。我建议你让 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 声明属于所有其他模型,而不是您可以通过订购位置来显示它在历史中的状态,这属于这批货物。

于 2013-04-06T15:07:43.613 回答