Skip to content

Instantly share code, notes, and snippets.

@vladzloteanu
Created September 27, 2012 11:39
Show Gist options
  • Save vladzloteanu/3793566 to your computer and use it in GitHub Desktop.
Save vladzloteanu/3793566 to your computer and use it in GitHub Desktop.
Mongoid persistence mode (fire and forget by default)
class MyModel
include Mongoid::Document
include Mongoid::Timestamps
field :value, :type => String
index :value, :unique => true
attr_accessible :value
end
MyModel.delete_all
MyModel.create_indexes
5.times{|t| puts MyModel.create!({:value => 'myvalue'}).inspect }
# #<MyModel _id: 50643bbc654af19b4a000093, value: "myvalue", updated_at: Thu Sep 27 11:42:52 UTC 2012, created_at: Thu Sep 27 11:42:52 UTC 2012, _type: nil>
# #<MyModel _id: 50643bbc654af19b4a000094, value: "myvalue", updated_at: Thu Sep 27 11:42:52 UTC 2012, created_at: Thu Sep 27 11:42:52 UTC 2012, _type: nil>
# #<MyModel _id: 50643bbc654af19b4a000095, value: "myvalue", updated_at: Thu Sep 27 11:42:52 UTC 2012, created_at: Thu Sep 27 11:42:52 UTC 2012, _type: nil>
# #<MyModel _id: 50643bbc654af19b4a000096, value: "myvalue", updated_at: Thu Sep 27 11:42:52 UTC 2012, created_at: Thu Sep 27 11:42:52 UTC 2012, _type: nil>
# #<MyModel _id: 50643bbc654af19b4a000097, value: "myvalue", updated_at: Thu Sep 27 11:42:52 UTC 2012, created_at: Thu Sep 27 11:42:52 UTC 2012, _type: nil>
# => 5
MyModel.all.to_a
# => [#<MyModel _id: 50643a81654af19b4a00000c, value: "myvalue", updated_at: Thu Sep 27 11:37:37 UTC 2012, created_at: Thu Sep 27 11:37:37 UTC 2012, _type: nil>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment