update_columns 更新了一个字符串,但是在 rails 上获取时它是 nil

update_columns update a string but it's nil when fetching on rails

我正在尝试将文件保存到 Google 存储桶,我遵循了 the official guide for rails

所以我有这个代码来更新我的文件

after_create :upload_document, if: :path 

  def upload_document
    file = Document.storage_bucket.create_file \
      path.tempfile,
      "cover_images/#{id}/#{path.original_filename}",
      content_type: path.content_type,
      acl: "public"
    # Update the url to my path field on my database
    update_columns(path: file.public_url)
  end

我可以将我的文件存储在我的存储桶中,我可以检索 public_url 并更新我的 table 上的路径字段,但是当我尝试获取路径字符串时,我有一个 nil.在我的 rails 控制台上举例

Document.find(14) 
=> #<Document id: 14, name: "Test", path: "https://storage.googleapis.com/xxxxxxxx-xxxx.x...", created_at: "2018-10-05 07:17:59", updated_at: "2018-10-05 07:17:59">
Document.find(14).path
=> nil
Document.find(14).name
=> "Test"

所以我不明白为什么我可以在使用 Rails 的 update_columns 进行更新后访问 SQL 数据库中的路径字段。

非常感谢您的帮助

您在 Document class(或包含的模块)上定义了一些覆盖默认属性访问器的方法。

要找出哪个,请在控制台中写入:

Document.find(14).method(:path).source_location

在任何情况下,您都可以直接使用

访问属性
Document.find(14)['path']