Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment