Ruby mongoid 问题

mjf429 · 2013年05月15日 · 最后由 mjf429 回复于 2013年05月15日 · 2945 次阅读

今天更新了 mongoid 的 master 版本,jbuilder 里的 id 全变成$oid,mongoid 的官方文档说

if you had not been converting your object ids to strings and were storing them in any way that would cause a Marshal dump, you may need to restore these values as strings

然后有个方案是

class Band
  include Mongoid::Document
  field :_id,
    type: String,
    pre_processed: true,
    default: ->{ Moped::BSON::ObjectId.new.to_s }
end

这样做怎么感觉很 2 啊,等于一个数据存了两份,而且 还得进行数据迁移。。。大家有更好的办法吗

用 jbuilder 解决了

def to_builder
  Jbuilder.new do |user|
    user.id id.to_s
  end
end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号