Skip to content

Tire result wrapping

tieleman edited this page Oct 23, 2012 · 2 revisions

Wrapping results in Tire

There's been a fair bit of discussion regarding the way Tire should retrieve objects from elasticsearch (ES) and present them to your Ruby code. Ultimately, there is no one correct way to do this due to the very different use cases that exist. Fortunately, Tire gives you a couple of options to customise the way this should be handled. This page serves as a summary and a how-to regarding to the different options available to the developer.

Using the standard wrapper (Tire::Results::Item)

This is the default behaviour of Tire. Any items you retrieve from ES are actually instances of Tire::Results::Item (see source). These instances fake being a different class for Rails (to allow routing and view templates) by overriding the #class method as such:

def class
  defined?(::Rails) && @attributes[:_type] ? @attributes[:_type].camelize.constantize : super
rescue NameError
  super
end

It attempts to deduce the correct (ActiveRecord) class from the _type attribute in ES. If that is not present, or we are not in Rails it will just return super.

Using the standard persistence options (Tire::Model::Persistence)

  • Pros and cons.

Using a custom wrapper

  • General info on why you would do such a thing
  • Using an object inheriting from SimpleDelegator