新手问题 mongoid update 返回 true,但没有更新成功

itsvoid · November 13, 2012 · Last by chenge replied at November 13, 2012 · 3520 hits

我有一个 model,名为 CustomSearchEngine 命令行下: irb(main):044:0> cse=CustomSearchEngine.last 返回实例,这没问题:

=> #<CustomSearchEngine _id: 50a1fc222061c78416000001, _type: nil, created_at: 2
012-11-13 07:52:59 UTC, updated_at: 2012-11-13 07:57:14 UTC, parent_id: nil, sta
tus: "draft", author_id: "50a047742061c77019000076", consumer_ids: [], node_id:
"508a3fee2061c76825000001">

注意 status:"draft" 然后,我执行 cse.update(status:"publish") 后,返回:

irb(main):045:0> cse.update(status:"publish")
=> true
irb(main):046:0> cse
=> #<CustomSearchEngine _id: 50a1fc222061c78416000001, _type: nil, created_at: 2
012-11-13 07:52:59 UTC, updated_at: 2012-11-13 07:57:14 UTC, parent_id: nil, sta
tus: "draft", author_id: "50a047742061c77019000076", consumer_ids: [], node_id:
"508a3fee2061c76825000001">

update 结果执行为 true,但是 status 还是 draft,这是何故?

mongoid 对 ruby 版本有要求么?

#1 楼 @chenge 我的版本是: ruby 1.9.3p125 (2012-02-16) [i386-mingw32] MongoDB shell version: 2.0.7

我现在这样做是可以更新的,不过比较怪异:

cse.status = 'publish'
cse.save

可查看 api 吧,用别人的东西,只好照别人的规矩。

不是 update_attribute 么

#4 楼 @stone update_attributes 我也用过,刚才我试了若干遍不行,重启了 rails c 后又可以了。不知什么原因。

update_attributes or update_attribute

#6 楼 @Rei update 也不报错,这是什么用法?

我测试 update 是可以的。好像不能带参数。

Person.where(first_name:'h232').update(last_name:'123x') 是可以的,好像要下一次访问才生效。

You need to Sign in before reply, if you don't have an account, please Sign up first.