重命名 Mongoid 中的字段不会更新 class 中的字段

Renaming field in Mongoid does not update field in class

当我重命名 Mongoid 中的字段时,它成功更新了 MongoDB 中的字段,但它没有更新 ruby 代码中的字段:

class Example
  include Mongoid::Document
  field :apple, type: String, as: :c_apple
end

Example.all
 => #<Mongoid::Criteria
  selector: {}
  options:  {}
  class:    Example
  embedded: false>

Example.all.rename("apple" => "d_apple")
  MOPED: 127.0.0.1:27017 UPDATE       database=core collection=examples selector={} update={"$rename"=>{"apple"=>"d_apple"}} flags=[:multi]
                         COMMAND      database=core command={:getlasterror=>1, :w=>1} runtime: 1.2460ms
 => {"connectionId"=>2, "updatedExisting"=>false, "n"=>0, "syncMillis"=>0, "writtenTo"=>nil, "err"=>nil, "ok"=>1.0} 

e = Example.new
 => #<Example _id: 5bfdbf103ed1492f9a000001, apple(c_apple): nil> 

 e.d_apple
NoMethodError: undefined method `d_apple' for #<Example _id: 5bfdbf103ed1492f9a000001, apple(c_apple): nil>

为什么 MongoDB 中的更改没有反映在 class 定义中?

当您 运行 #rename 时,您会与 MongoDB 互动。文档结构的变化存储在数据库中。该方法不与ruby源码交互,需要自行编辑。

顺便说一句。我不记得有任何 ruby/rails 方法(除了 rake 任务和生成器中的方法)与存储在文件中的源代码交互。