ohm

Many-to-many relationships with Ruby, Redis, and Ohm

霸气de小男生 提交于 2019-12-10 12:21:42
问题 I'm attempting to create a many-to-many relationship in Redis using Ohm. As an example, I have Book and Author models defined as follows: class Book < Ohm::Model attribute :title set :authors, Author end class Author < Ohm::Model attribute :last_name attribute :first_name set :books, Book end What I would like to be able to do is leverage Ohm's indexing capabilities to do finds such as: require 'test_helper' class ManyToManyRelationshipTest < ActiveSupport::TestCase setup do @dave_thomas =

Setting a dynamic field in Ohm / Redis

社会主义新天地 提交于 2019-12-10 11:18:31
问题 How do I set a field dynamically for a Ohm object? class OhmObj < Ohm::Model attribute :foo attribute :bar attribute :baz def add att, val self[att] = val end end class OtherObj def initialize @ohm_obj = OhmObj.create end def set att, val @ohm_obj[att] = val #doesn't work @ohm_obj.add(att, val) #doesn't work end end 回答1: The attribute class method from Ohm::Model defines accessor and mutator methods for the named attribute: def self.attribute(name) define_method(name) do read_local(name) end