Mongoid update_attributes 没有得到持久化

发布于 2024-11-06 06:21:11 字数 1321 浏览 0 评论 0原文

这是一个简单的模型。

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

2.知道了可以找到该事件

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

3.所以更新事件属性

ruby-1.9.2-p0 > event.update_attributes!(:name => "new name")
 => true 

4.让我们尝试查找该事件

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.糟糕,没有找到,但旧的事件仍然存在

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


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

Here is a simple Model.

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.We create and event

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

2.Know lets find the event

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

3.So update event attributes

ruby-1.9.2-p0 > event.update_attributes!(:name => "new name")
 => true 

4.Lets try to find the event

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 not found, but the old one is still persisted

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

What I am doing wrong ? I hope this is not a bug.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不交电费瞎发啥光 2024-11-13 06:21:11

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

Mod on _id not allowed

每当您需要更改 name 字段时,您'您可能需要:

  1. 将其复制到具有新名称的新记录。
  2. 删除旧记录

I don't believe MongoDB lets you alter an _id field. When I try it using the standard mongo shell, I get this error (meaning it's not a Mongoid limitation, it's a limitation in the actual Mongo software):

Mod on _id not allowed

Whenever you need to alter the name field, you'll probably need to:

  1. Copy it to a new record with the new name.
  2. Delete the old record
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文