Skip to content

Instantly share code, notes, and snippets.

@chanks
Created November 13, 2010 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 => 'example@example.com'
user = User.create # => #<User _id: 4cdee1080de0690f96000002, email: nil, name: nil>
user.update_attributes :email => 'example@example.com' # false
user.errors # => {:email=>["is already taken"]}
user.attributes # => {"_id"=>BSON::ObjectId('4cdee1080de0690f96000002'), "email"=>"example@example.com"}
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: "example@example.com", name: "Chris">
# Uh-oh!
User.where(:email => 'example@example.com').count # => 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment