3
這裏是一個簡單的模型。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.Know可以找到事件
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更新事件屬性
ruby-1.9.2-p0 > event.update_attributes!(:name => "new name")
=> true
4.Lets嘗試找到事件
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沒有找到,但舊的一人仍然堅持
ruby-1.9.2-p0 > event = Event.find("event1")
=> #<Event _id: event1, _type: nil, _id: "event1", name: "event1", schedule: nil, description: nil, active: true>
我做錯了嗎?我希望這不是一個錯誤。
是的壞沒有在文檔,我決定讓這個,我只是檢查是否關鍵是得到更新,如果是複製文件,更改密鑰,保存新doc和刪除舊的,這樣用戶得到正是他想要的東西。 – daniel 2011-05-12 03:53:26