2010-11-25 17 views
2
Account私はモデル Accountを持っています:

Rails、Mongoid:カスタムキーとカスタムキーフォーマットを使用する

class Account 
    include Mongoid::Document 
    include Mongoid::Timestamps 
    ... 
end 

特定のフォーマットで特定のIDを使用したいと思います。私はidが4ceede9b5e6f991aef000007の代わりに16桁になるようにしたい、そのようなもの:1111222233334444

これを実行するベストプラクティスは何ですか?

答えて

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を行うことができます。

関連する問題