我有一个用户名作为字段的集合。模型将此字段定义为唯一。但是我能够在数据库中插入重复的值。
class Profile
include Mongoid::Document
include Mongoid::Paperclip
field :username
index({ username: 1 } , { unique: true })
end
但是Collection有两个相同的用户名
{ "_id" : ObjectId( "50b3b323421aa95da6000004" ),
"username" : "marceloreuse" }
{ "_id" : ObjectId( "50b3b567421aa93d84000002" ),
"username" : "marceloreuse" }
这里出了什么问题?
答案 0 :(得分:9)
我会仔细检查您的索引 - 从控制台尝试db.collection.getIndexes()
并确保您的索引存在。
如果您错过了它,Mongoid不会因为您指定它而自动构建索引 - 您需要运行包含的内容:rake db:mongoid:create_indexes
。