soveran / ohm

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

Index on multiple attributes? #198

Closed kapso closed 9 years ago

kapso commented 9 years ago

Can you create an index on multiple attributes? So something like this...

class Propect < Ohm::Model
  attribute :first_name
  attribute :last_name
  attribute :bio

  index [:first_name, :last_name]
end

Prospect.find(first_name: "Jeff", last_name: "Cad") # Uses index?
Prospect.find(first_name: "Jeff") # Uses index?
kapso commented 9 years ago

Figured thus out, I will have to define

index :first_name
index :last_name
soveran commented 9 years ago

Yes, that's the way to define multiple indices. Note that any method can be indexed, so if you ever have a need of a composed index, you can build it like this:

class Propect < Ohm::Model
  attribute :first_name
  attribute :last_name

  index :full_name

  def full_name
    "#{first_name} #{last_name}"
  end
end

Prospect.find(full_name: "Jeff Cad")

Of course full_name is not the best use case for a composed index! I just wanted to mention the idea just in case you ever need it.