【问题标题】:Is it possible to update model attributes using Ohm and Redis DB is Ruby?是否可以使用 Ohm 更新模型属性,而 Redis DB 是 Ruby?
【发布时间】:2010-11-20 03:41:42
【问题描述】:

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

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

【问题讨论】:

    标签: ruby gem redis ohm


    【解决方案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属性被更新了。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2022-11-27
        • 2021-11-12
        • 1970-01-01
        • 1970-01-01
        • 2021-05-07
        • 2012-02-12
        • 1970-01-01
        • 2021-12-17
        • 1970-01-01
        相关资源
        最近更新 更多