Created
November 13, 2010 19:10
-
-
Save chanks/675555 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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