Mayuresh Srivastava
Mayuresh Srivastava

Reputation: 1422

Result of where query in rails

I am performing a db search as below:

abc = params[:search]      
Model.where("column_name ilike ?", "%#{abc}%")

When I am searching nothing it is running like below:

Model.where("column_name ilike ?", "%%")

which is giving me all rows of table.

When my search param is empty I want it to run like:

Model.where("column_name ilike ?", "") 

How can I achieve that?

Upvotes: 2

Views: 117

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52357

Well, just check whether the params is an empty string:

Model.where("column_name ilike ?", abc.blank? ? '' : "%#{abc}%")
# or, more strict check
Model.where("column_name ilike ?", abc == '' ? '' : "%#{abc}%")

Upvotes: 2

Related Questions