Skip to content

Instantly share code, notes, and snippets.

@chanks
Created November 13, 2010 19:10
Show Gist options
  • Save chanks/675555 to your computer and use it in GitHub Desktop.
Save chanks/675555 to your computer and use it in GitHub Desktop.
require 'mongoid'
Mongoid.configure { |config| config.master = Mongo::Connection.new.db 'mongoid_test' }
class User
include Mongoid::Document
field :email
field :name
validates_uniqueness_of :email
end
User.create :email => '[email protected]'
user = User.create # => #<User _id: 4cdee1080de0690f96000002, email: nil, name: nil>
user.update_attributes :email => '[email protected]' # false
user.errors # => {:email=>["is already taken"]}
user.attributes # => {"_id"=>BSON::ObjectId('4cdee1080de0690f96000002'), "email"=>"[email protected]"}
user.errors.clear # => {}
user.reload # => #<User _id: 4cdee1080de0690f96000002, email: nil, name: nil>
user.update_attributes :name => 'Chris' # => true
user.attributes # => {"_id"=>BSON::ObjectId('4cdee1080de0690f96000002'), "name"=>"Chris"}
# All as expected, until...
user.reload # => #<User _id: 4cdee1080de0690f96000002, email: "[email protected]", name: "Chris">
# Uh-oh!
User.where(:email => '[email protected]').count # => 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment