Mongoid update_attributes 没有得到持久化
这是一个简单的模型。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不相信 MongoDB 允许您更改
_id
字段。当我使用标准 mongo shell 尝试它时,我收到此错误(这意味着它不是 Mongoid 限制,而是实际 Mongo 软件中的限制):每当您需要更改
name
字段时,您'您可能需要: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):Whenever you need to alter the
name
field, you'll probably need to: