无法对MongoMapper模型执行重置操作,无法从模型中删除/删除密钥 [英] Unset operation failing for MongoMapper model, cannot delete / remove key from model

查看:54
本文介绍了无法对MongoMapper模型执行重置操作,无法从模型中删除/删除密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用mongodb 2.0.0,mongo gem 1.4.1,mongo_mapper 0.9.2,rails 3.0.6.

We're on mongodb 2.0.0, mongo gem 1.4.1, mongo_mapper 0.9.2, rails 3.0.6.

我们喜欢MongoMapper,但我们需要帮助解决一个棘手的问题:我们从一些测试中保留了一个密钥,但是调用obj.unset却无济于事.

We love MongoMapper, but we need helping resolving one nasty issue: we have a key carried over from some testing, but invoking obj.unset fails to do anything.

具体来说,我们正在尝试删除"id"键(而不是"_id"),因为它导致MM将obj.id与不需要的obj._id区别对待.

Specifically, we are trying to remove an "id" key (not "_id") because it's causing MM to treat obj.id as different from obj._id, which we don't want.

清除数据库后,我们从一个没有执行其他操作的控制器中运行了这些命令:(我们还尝试从rails控制台运行相同的代码,但是也失败了.)

After clearing out the database, we ran these commands from a controller which does nothing else: (We also tried running the same code from the rails console, but it also fails.)

logger.info "#{Game.keys.keys.inspect}"
Game.unset({}, :id)
logger.info "#{Game.keys.keys.inspect}"
Game.unset(:id)
logger.info "#{Game.keys.keys.inspect}"

输出:

["jackpot", "players", "created_at", "puzzles", "ended_at", "player_index", "updated_at", "log", "_id", "id", "join_code", "puzzle_index"]
["jackpot", "players", "created_at", "puzzles", "ended_at", "player_index", "updated_at", "log", "_id", "id", "join_code", "puzzle_index"]
["jackpot", "players", "created_at", "puzzles", "ended_at", "player_index", "updated_at", "log", "_id", "id", "join_code", "puzzle_index"]

我们的游戏模型中定义的当前键:

Current keys defined in our Game model:

  key :players, Array, :default => []
  key :player_index, Integer, :default => 0
  key :puzzles, Array, :default => []
  key :puzzle_index, Integer, :default => 0
  key :join_code, String, :default => nil
  key :jackpot, Integer, :default => 0
  key :log, Array, :default => []
  key :created_at, Time
  key :updated_at, Time
  key :ended_at, Time, :default => nil

帮助?

谢谢!

推荐答案

我们很难发布答案,因为这巩固了我们的傻瓜,白痴,傻瓜,菜鸟,杰伊·莱诺粉丝"的地位,但如果有人碰碰遇到同样的问题:虽然我们的模型目录在开发环境中是干净的,但生产环境中的模型目录包含旧的测试文件...包含带有"id"键的旧模型.

It pains us to post the answer since this solidifies our status as "morons, idiots, fools, noobs, Jay Leno fans," but in case anyone else bumps into the same issue: while our model directory was clean in the dev environment, the model dir in the production environment contained old test files ... which contained an old model with the "id" key.

显然,删除旧文件和旧模型可以解决所有问题,尽管我们的自负和头颅(从墙上的过度撞击)留下了巨大的伤痕.

Obviously, removing the old files and the old models solved everything, though we're left with staggering bruises to our egos and to our heads (from excessive banging against the walls).

这篇关于无法对MongoMapper模型执行重置操作,无法从模型中删除/删除密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆