ruby-on-rails - 如何从 ruby on rails 应用程序中查询 UPDATE 表?
问题描述
在我Document.last.files.first.purge
从控制台使用 rails c 进行测试时,第一个文件被删除,但 rails c 也会自动对文档调用 UPDATE 查询。但是当我从我的应用程序中对文件调用 purge 时,我可以看到在 rails 服务器中只启动了 DELETE 查询。(这会导致一堆问题,比如服务器试图加载不存在的 blob,所以我认为在对表进行更改后更新表非常重要。)我的问题是如何从 ruby 复制这个 UPDATE 查询?谢谢!
doc= Document.last
删除文件的代码:
控制器(documents_controller.rb)
def delete_file_attachment
@file = ActiveStorage::Blob.find_signed(params[:id]).purge
end
查看 (_form.html.erb)
<div class="files">
<% @document.files.each do |file| %>
<%= link_to file.filename, delete_file_attachment_document_url(file.signed_id), method: :delete, data: { confirm: 'Are you sure?' }%>
<% end %>
</div>
模型(document.rb)
class Document < ApplicationRecord
has_many_attached :files
end
解决方案
尝试在表单上使用 where 子句,并使用 update_all() 为该特定事件运行 SQL 查询。看看这样的东西是否适用于您的数据库 - >
Document.where(conditions).limit(1).update_all(changes) # => 1
这是 Active Record 关系中有关 update_all() 的一些文档 https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-update_all
推荐阅读
- html - 修复了在内容浏览器页脚上滚动时的页眉和页脚设计
- python - Keras – 人工神经网络 – 使用自定义激活函数时出错
- java - 尝试设置标签(Javafx)不断收到“无法调用”javafx.scene.control.Label.setText(String)“”错误消息
- android - 在 Android Studio 中运行以下代码后获取位置坐标的 Nullpointer 异常
- hcl - HCL2 不支持时间戳功能吗?
- virtual-machine - Qemu-system-mips 在“启用控制台 [tty0]”后卡住
- android - 不存在类型变量 D 的实例,因此 Cart 符合 LoaderCallbacks
错误 - vue.js - 如果已选择日期,如何处理 QDate 点击事件
- python - 如何在张量流中选择特定元素?
- azure-data-lake-gen2 - 关于 dataverse 复制,使用 Data Export Service 和 Export to Data Lake 有什么区别?