Skip to content

Instantly share code, notes, and snippets.

@arturopie
Created December 16, 2016 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturopie/7d78cb20c0033c62f7a628e03dab72f1 to your computer and use it in GitHub Desktop.
Save arturopie/7d78cb20c0033c62f7a628e03dab72f1 to your computer and use it in GitHub Desktop.
conditions_strings = []
conditions_params = {}
unless filters[:comment_text].blank?
conditions_strings << "comments.text ILIKE :comment_text"
conditions_params[:comment_text] = "%#{filters[:comment_text]}%"
end
conditions = (conditions_params.empty? ? "" : [conditions_strings.join(" AND "), conditions_params])
joins_list = []
joins_list << {:posts => :comment} if filters[:comment_text].present?
posts = Post.where(conditions).joins(joins_list).order("expected_ship_at desc, id desc")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment