我对 ROR 相当陌生,正在从事货物跟踪项目。这个想法是记录对发货位置的所有更改,以便当用户使用跟踪号跟踪他们的发货时,他们会看到当前位置和经过的位置历史记录。
我有一个 shipping 表和一个 shipping_locations 表。
出货量
+--------------------------+--------------+------+-----+---------+----------------+ | 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 | | +--------------------------+--------------+------+-----+---------+----------------+
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 | | +------------------+--------------+------+-----+---------+----------------+
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_update
AFTER 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()); 结束$$
分隔符;