Query Objects

  • Connecting more complex SQL statement together
  • Can be placed in /app/queries
class ShoutSearchQuery
  def initialize(term:)
    @term = term
  end

  def to_relation
    Shout.joins(
      'LEFT JOIN text_shouts ON content_type = "TextShout" AND content_id = text_shouts.id',
      )
      .where('text_shouts.body LIKE ?', "%#{term}%")
      .order(created_at: :desc)
  end

  private

  attr_reader :term
end