Ohm inside tricks

ohm redis monk

05|06|2010

Did you know #1

I was reading Ohm's tests and stumbled upon a cool testcase, which led me to learn about a cool feature.

class User < Ohm::Model
  attribute :email

  index :email_provider

  def email_provider
    email.split('@').last
  end
end

# So you can basically do this:
User.find(email_provider: 'yahoo.com')
User.find(email_provider: 'gmail.com')

Pretty neat eh?

Did you know #2

Now I was talking to soveran at the #ohm irc channel on freenode while I stumbled upon #1 above, and he went on to add that you can index any enumerable object.

This here is doubly cool!

class Item < Ohm::Model
  attribute :tag_list

  index :tags

  def tags
    tag_list.split(/\s+/)
  end
end

# Now you got yourself a minimalistic tagging solution!

Item.create(:tag_list => 'ohm monkrb redis')

Item.find(tags: 'ohm')
Item.find(tags: 'monkrb')
Item.find(tags: 'redis')
blog comments powered by Disqus