-
Notifications
You must be signed in to change notification settings - Fork 0
Tire result wrapping
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.
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
.
- Pros and cons.
- General info on why you would do such a thing
- Using an object inheriting from
SimpleDelegator