3
唯一我切換到Mongoid 3,這使得一些事情不同:)目前,我嘗試檢查一個複合域是唯一的: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
如何獲得其中的validates_uniqueness_of其應該檢查ip和端口是否是唯一的複合字段? AFAIK有在Mongoid 2的方式基於多個領域創造了新的_id,但現在看來,這是在Mongoid 3中刪除:在3輾轉
key :ip, :port
非常感謝您的答覆。很多東西在Mongoid 3中改變了:)但是它似乎是我正在尋找的東西。 – ctp 2012-08-22 12:11:50
在上面的代碼中有一個語法錯誤,正確的代碼是:''field:_id,type:String,default: - > {ip +「:」+ port}''(默認情況下注意冒號) d已經編輯了原始答案,但是有一個6字符的帽子。 – wintersolutions 2013-05-10 16:54:42
修正了它。謝謝。 – Vickash 2013-05-11 00:59:13