Mongomapper throwing error message multiple times

81 Views Asked by At

I have a simple Model - Shop in my Ruby/MongoMapper application.

I have no idea why sometimes I only get one error message, and sometimes I get three error messages like this one:

'Validation failed: User has already been taken, User has already been taken, User has already been taken'

Yes, I am adding the same user_id multiple times, on purpose.

Here is my code:

class Shop
  include MongoMapper::Document
  key :user_id, String, :required => true, :unique => true

  def self.add
    begin
      ut = Shop.new
      ut.user_id = '11'
      ut.save!
      ut = nil
    rescue Exception => e
      puts e.message
    end
  end
end

Result:

# 'Validation failed: User has already been taken, User has already been taken, User has already been taken'

UPDATE:

Seems like removing the

ut = nil

fixes the 'problem'. But why...

0

There are 0 best solutions below