soveran / ohm

Object-Hash Mapping for Redis

Home Page:http://ohm.keyvalue.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Index on multiple attributes?

kapso opened this issue · comments

commented

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?
commented

Figured thus out, I will have to define

index :first_name
index :last_name

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.