我有模型Account
:
class Account
include Mongoid::Document
include Mongoid::Timestamps
...
end
我想使用特定格式的特定ID。我希望id为16位而不是4ceede9b5e6f991aef000007
,类似于:1111222233334444
。
这样做的最佳做法是什么?
答案 0 :(得分:2)
如果id是一个简单的数字,请尝试:
class Account
include Mongoid::Document
include Mongoid::Timestamps
identity :type => Integer
end
account = Account.new :id => 1111222233334444
#=> #<Account _id: 1111222233334444, created_at: nil, updated_at: nil>
account.save
#=> true
account
#=> #<Account _id: 1111222233334444, created_at: 2010-11-26 00:48:27 UTC, updated_at: 2010-11-26 00:48:27 UTC>
Account.count
#=> 1
Account.first
#=> #<Account _id: 1111222233334444, created_at: 2010-11-26 00:48:27 UTC, updated_at: 2010-11-26 00:48:27 UTC>
如果您也想在ID中使用字母,则可以改为identity :type => String
。