Search code examples
rubyrubygemsredisohm

Is it possible to update model attributes using Ohm and Redis DB is Ruby?


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!


Solution

  • 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.