我是ROR的新手,正在开展货物跟踪项目。 我们的想法是将所有更改记录到装运地点,以便当用户使用跟踪编号跟踪他们的货件时,他们会看到当前位置和通过位置的历史记录。
我有货运表和shipment_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 | |
+--------------------------+--------------+------+-----+---------+----------------+
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 | |
+------------------+--------------+------+-----+---------+----------------+
Shipment_history(建议)
字段:
我想将来自shipment_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
DELIMITER $$
DROP TRIGGER shipment_after_update
$$
shipment_after_update
更新后创建触发器shipments
对于每一行
开始
INSERT INTO cargo_transit_histories
(shipment_id
,current_location
,final_location
,updated_at
)
VALUES
(NEW.id,NEW.current_location,NEW.final_location,NOW());
END $$
DELIMITER;
答案 0 :(得分:0)
我不确定我是否帮助你,如果这是一个好方法但我会尝试。 我建议你使shipment_history属于shipment_locations。
因此,如果shipment_history有shipment_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声明的所有其他模型,并且您可以通过订购属于此货件的位置来显示历史状态。 / p>