是否可以使用Ohm更新模型属性,Redis DB是Ruby?

时间:2009-08-17 05:33:10

标签: ruby gem redis ohm

我首先看一下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"

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

2 个答案:

答案 0 :(得分:1)

您应该可以使用常规#save来完成此操作。你能发布更多的背景知道它为什么不起作用吗?


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

答案 1 :(得分: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属性。