Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arthurariza/c8eb1b234c93b9cf98559317a8712755 to your computer and use it in GitHub Desktop.
Save arthurariza/c8eb1b234c93b9cf98559317a8712755 to your computer and use it in GitHub Desktop.
Dynamically Built Where Clauses
conditions = {}
conditions.merge!(published: params[:published]) if params[:published]
conditions.merge!(author: params[:author]) if params[:author]
conditions.merge!(author: params[:author]) if params[:author]
conditions.merge!(created_at: params[:created_at]) if params[:created_at] # If the params[:created_at] is a DateTime
# Use spat operator as below
@posts = Post.where(**conditions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment