Mongomapper人类可读的id

时间:2014-09-11 09:30:13

标签: ruby-on-rails mongomapper

有没有办法为mongomapper创建人类可读的id 我的意思是将54114ac51d41c84fb900005a替换为id 1

由于

1 个答案:

答案 0 :(得分:0)

尝试:

gem 'mongomapper_id2'

这是MongoMapper的插件,可以在MongoMapper的文档中添加auto incremented ids

示例:

# app/models/movie.rb
class Movie
  include MongoMapper::Document

  key :title, String 

  # Here is the mongomapper_id2
  auto_increment!
end

movie = Movie.create(:title => 'The Simpsons Movie')
movie.id # BSON::ObjectId('4d1d150d30f2246bc6000001') 

# Here is the mongomapper_id2
movie.id2
# => 1

movie2 = Movie.create(:title => 'Pirates of Silicon Valley')

movie2.id2
# => 2