Ahmad hayat
Ahmad hayat

Reputation: 99

Search result is not displaying

I have an issue in searching records from the PostgreSQL with particular search keyword but no record is displaying here is the code

 filter_text=params[:filter_search]
 @outputs = Output.where("name LIKE '%#{filter_text}%'").order("name ASC")

Upvotes: 5

Views: 577

Answers (5)

titan
titan

Reputation: 676

If you use ransack gem, it will allow you to use simple methods to search. Using ransack, you will only need to do this:

@outputs = Output.search(name_cont: params[:filter_search]).result.order("name ASC")

Upvotes: 3

孙悟空
孙悟空

Reputation: 1285

Try this :

    filter_text=params[:filter_search] 
    @outputs = Output.where("name LIKE ?","%#{filter_text}%").order("name ASC")

Upvotes: 7

Fakhir Shad
Fakhir Shad

Reputation: 1101

One easy way to search is to use Ransack. Which provides you an efficient search mechanism.

Upvotes: 1

oj5th
oj5th

Reputation: 1409

Instead of:

filter_text=params[:filter_search]
@outputs = Output.where("name LIKE '%#{filter_text}%'").order("name ASC")

Try the following:

filter_text=params[:filter_search]
@outputs = Output.where(["name LIKE ?", "%#{filter_text}%"]).order("name ASC")

Upvotes: 3

Deepak Mahakale
Deepak Mahakale

Reputation: 23711

If you are going for case insensitive search go for ILIKE

filter_text = params[:filter_search]
@outputs = Output.where("name ILIKE ?", "'%#{filter_text}%'").order("name ASC")

Upvotes: 3

Related Questions