2011-05-12 53 views
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> 


我做錯了嗎?我希望這不是一個錯誤。

回答

3

我不相信MongoDB中,您可以改變一個_id場。當我嘗試使用標準蒙戈外殼,我得到這個錯誤(這意味着它不是一個Mongoid限制,它在實際蒙戈軟件的限制):

Mod on _id not allowed 

每當你需要改變name領域,你」您可能需要:

  1. 將它複製到一個新的記錄與新的名稱。
  2. 刪除舊的記錄
+0

是的壞沒有在文檔,我決定讓這個,我只是檢查是否關鍵是得到更新,如果是複製文件,更改密鑰,保存新doc和刪除舊的,這樣用戶得到正是他想要的東西。 – daniel 2011-05-12 03:53:26