Mongoid update_attributes not saved

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. Now recognize 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. Report update event attributes

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

4. Let's try to find an 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 preserved

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 am I doing wrong? Hope this is not a mistake.

+3
source share
1 answer

I do not believe that MongoDB allows you to change the field _id. When I try to use the standard mongo shell, I get this error (this is not a Mongoid restriction, this is a restriction in real Mongo software):

Mod on _id not allowed

Whenever you need to change a field name, you will probably need:

  • Copy it to a new record with a new name.
  • Delete old post
+3
source

All Articles