Sha256: b15cb84793abbb5605477f0c63fe65fd1f753f8d5b5451a954820a85fa6b9c3b
Contents?: true
Size: 1.16 KB
Versions: 8
Compression:
Stored size: 1.16 KB
Contents
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__)) require 'mongo_mapper' MongoMapper.database = 'testing' class User include MongoMapper::Document key :email, String end # Drop collection and ensure unique index on email User.collection.drop User.ensure_index(:email, :unique => true) User.create(:email => 'nunemaker@gmail.com') User.create(:email => 'nunemaker@gmail.com') puts "Count should only be one since the second create failed, count is: #{User.count}" # 1 because second was not created, but no exception raised puts # save method also takes options, including :safe # which will force raise when duplicate is hit begin user = User.new(:email => 'nunemaker@gmail.com') user.save(:safe => true) rescue Mongo::Error::OperationFailure => e puts 'Mongo Operation failure raised because duplicate email was entered' puts e.inspect puts end # Mark user model as safe, same as doing this... # class User # include MongoMapper::Document # safe # end User.safe begin User.create(:email => 'nunemaker@gmail.com') rescue Mongo::Error::OperationFailure => e puts 'Mongo Operation failure raised because duplicate email was entered' puts e.inspect end
Version data entries
8 entries across 8 versions & 1 rubygems