1

我首先看一下 Monk 和 Ohm/Redis API,我有一个简单的问题。是否可以使用 Ohm/Redis 更新模型对象的属性?

class Event < Ohm::Model
  attribute :name
  index :name
end

Event.create(:name => "A mistake made here...")

@event = Event.find(:id, 25)
@event.name = "I want to edit my mistake... but do not know how"
@event.save

使用 Ohm API,我可以执行以下操作

require 'ohm'
Ohm.connect
Ohm.redis.set :foo, "bar"
Ohm.redis.set :foo, "bat"

似乎无法在文档中找到有关如何完成此操作的任何信息。提前致谢!

4

2 回答 2

1

您应该能够使用常规的#save. 您可以发布更多上下文以找出它不起作用的原因吗?


event = Event[25]
event.name = "Updated name"
event.save

于 2009-09-16T16:06:54.603 回答
1

我不确定我是否完全理解您的要求,但是使用以下代码更新了属性。

require 'rubygems'
require 'ohm'

Ohm.connect

class Event < Ohm::Model
  attribute :name
  index :name
end

Event.create(:name => "A mistake made here...")

@event = Event.find(:name => "A mistake made here...").first
puts @event.inspect
@event.name = "I want to edit my mistake... but do not know how"
@event.save
puts @event.inspect

@event2 = Event.find(:name => "I want to edit my mistake... but do not know how").first
puts @event2.inspect

然后我得到:

#<Event:1 name="A mistake made here...">
#<Event:1 name="I want to edit my mistake... but do not know how">
#<Event:1 name="I want to edit my mistake... but do not know how">

所以 name 属性被更新了。

于 2009-12-21T13:20:26.473 回答