Ruby on rails 3 Mongoid更新_属性未得到持久化

Ruby on rails 3 Mongoid更新_属性未得到持久化,ruby-on-rails-3,mongoid,Ruby On Rails 3,Mongoid,这是一个简单的模型 class Event include Mongoid::Document field :name, type: String field :schedule, type: String field :description, type: String field :active, type: Boolean, default: true key :name end 1.我们创建和维护活动 ruby-1.9.2-p0 > event = E

这是一个简单的模型

class Event
  include Mongoid::Document

  field :name, type: String
  field :schedule, type: String
  field :description, type: String
  field :active, type: Boolean, default: true
  key :name

end
1.我们创建和维护活动

ruby-1.9.2-p0 > event = Event.new(:name => "event1")
 => #<Event _id: event1, _type: nil, _id: "event1", name: "event1", schedule: nil, description: nil, active: true> 
ruby-1.9.2-p0 > event.save!
 => true
4.让我们试着找到事件

ruby-1.9.2-p0 > event = Event.find("event1")
=> #<Event _id: event1, _type: nil, _id: "event1", name: "event1", schedule: nil, description: nil, active: true> 
ruby-1.9.2-p0 > event = Event.find("new name")
Mongoid::Errors::DocumentNotFound: Document not found for class Event with id(s) new name.
5.未找到Ooops,但旧的Ooops仍然存在

ruby-1.9.2-p0 > event = Event.find("event1")
 => #<Event _id: event1, _type: nil, _id: "event1", name: "event1", schedule: nil, description: nil, active: true>
ruby-1.9.2-p0>event=event.find(“event1”)
=> #


我做错了什么?我希望这不是一个bug。

我不相信MongoDB允许您更改
\u id
字段。当我使用标准mongo shell尝试它时,我得到了这个错误(这意味着它不是Mongoid限制,而是实际mongo软件中的限制):

每当您需要更改
名称
字段时,您可能需要:

  • 将其复制到具有新名称的新记录中
  • 删除旧记录

  • 是的,坏的不在文档中,我决定允许这样做,我只是检查密钥是否得到更新,如果是这样,复制文档,更改密钥,保存新文档并删除旧文档,这样用户就可以得到他想要的。
    ruby-1.9.2-p0 > event = Event.find("event1")
     => #<Event _id: event1, _type: nil, _id: "event1", name: "event1", schedule: nil, description: nil, active: true>
    
    Mod on _id not allowed