我的数据模型有可能带有附件的会议。我的update_meeting API包含以下几行(
):attachment = Attachment.new(image: params[:attachment], user: current_user)
begin
attachment.save!
@meeting.attachments << attachment
NotificationManager.add_attachment_to_meeting(current_user, @meeting, attachment)
rescue
puts "Error occured trying to upload attachment: #{attachment.inspect}, #{attachment.errors.inspect}"
end
我们遇到了一种特殊情况,即附件已创建但未与数据库中的会议关联。我开始挖掘日志以了解发生了什么。我看不到任何错误,异常,回滚等等。特别是这三行:
(0.7ms) BEGIN
SQL (1.0ms) UPDATE "attachments" SET "updated_at" = $1, "meeting_id" = $2 WHERE "attachments"."id" = $3 [["updated_at", 2018-07-06 13:57:52 UTC], ["meeting_id", 434], ["id", 51]]
(1.3ms) COMMIT
我的假设(在看到之前)是,如果后面有一个COMMIT的UPDATE语句而没有ROLLBACK或错误消息,或者日志中的任何内容都应该成功。但显然不是。
有人在这可能出错的地方有任何指针吗?我完全不知道从哪里开始寻找东西。不确定如何重现,不确定要添加什么日志消息以使其在下次发生时更容易,不确定...
我正在使用在EC2上运行的Rails 5和PostgreSQL。
答案 0 :(得分:2)
让会议创建附件
begin
attachment = @meeting.attachments.create(image: params[:attachment], user: current_user)
NotificationManager.add_attachment_to_meeting(current_user, @meeting, attachment)
rescue
puts "Error occured trying to upload attachment: #{attachment.inspect}, #{attachment.errors.inspect}"
end
会议应像附件的“聚合器”。附件不应该存在,或者至少在没有开会的情况下就不存在,因此应该通过会议创建