我切换到Mongoid 3,这使得一些不同的东西:)目前我尝试检查复合字段是否是唯一的:
class Host
include Mongoid::Document
field :ip, :type => String
field :port, :type => Integer
field :username, :type => String
field :password, :type => String
validates_presence_of :ip
validates_presence_of :port
end
Run Code Online (Sandbox Code Playgroud)
如何在其中获取validates_uniqueness_of,检查ip和port是否是唯一的复合字段?AFAIK在Mongoid 2中有一种方法可以基于多个字段创建一个新的_id,但似乎在Mongoid 3中删除了它:
key :ip, :port
Run Code Online (Sandbox Code Playgroud)