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

发布于 2024-08-02 05:31:27 字数 493 浏览 4 评论 0原文

我第一次了解 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"

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

I'm taking a first look at Monk and the Ohm/Redis APIs and I have a simple question. Is it possible to update attributes on model objects using 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

Using the Ohm API I can do the following

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

Can't seem to find any info in the docs about how to accomplish this. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彩虹直至黑白 2024-08-09 05:31:27

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

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">

所以名称属性已更新。

I'm not sure I fully understand what you are asking about, but with the following code the attribute is updated.

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

I then get:

#<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">

So the name attribute is updated.

我也只是我 2024-08-09 05:31:27

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


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

You should be able to do it using a regular #save. Can you post more context to find out why it's not working?


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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文