soveran / ohm

Object-Hash Mapping for Redis
http://ohm.keyvalue.org
MIT License
1.4k stars 167 forks source link

Feature Suggestion: `Ohm::Model::fetch` for uniques? #232

Open sirscriptalot opened 6 years ago

sirscriptalot commented 6 years ago

Hi all,

I was curious if anyone thought a method that fetches records by a unique and an array of values would be useful on Ohm::Model? It would look something like...

    # I'm not a fan of the method name...
    def self.fetch_with(att, values)
      raise Ohm::IndexNotFound unless uniques.include?(att)

      ids = nil
      unique = key[:uniques][att]

      synchronize do
        values.map { |value| redis.queue('HGET', unique, value) }

        ids = redis.commit
      end

      return [] if ids.nil?

      fetch(ids)
    end

There is a point in the application I'm working on that uses something similar, and just wanted to share incase you all might find a generic version of it it useful for everyone.

soveran commented 6 years ago

I love the comment about not being a fan :-)

I think this is interesting. Are you still using it?

sirscriptalot commented 6 years ago

There is a couple of points I am using something very similar to what I wrote above. Basically the app has some light social features and an example where it can be useful is to look up a collection of users via unique nicknames instead of id.