Search code examples
ruby-on-railsactiverecordactivemodelactive-model-serializers

Patterns for dealing with multi model / single collection queries


I am writing a search endpoint in Rails that takes one search term and returns multiple models in one JSON response. Pagination is required. ActiveModelSerializer is used for encoding objects to JSON.

Is there an established pattern for dealing with situations like this?


Solution

  • Your question is a tad ambiguous, but from the looks of things you'd definitely want to use a Service Object for your search functionality, which is best when you're reaching across multiple models. Here are links to two popular articles that can explain Service Objects better than I can:

    http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/

    http://sporto.github.io/blog/2012/11/15/a-pattern-for-service-objects-in-rails/