Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails search SQL query

I am trying to make search method in my Rails app, but sadly - my SQL knowledge is rather pathetic. I managed to find guides that have searched text in the description by:

@guides = Guide.where(['description LIKE ?', "%#{params[:search]}%"])

or in the title:

 @guides = Guide.where(['title LIKE ?', "%#{params[:search]}%"])

but I don't know how to look in both title and descripton at the same time. Please help me solve this problem.

Additionally I would appreciate any suggestions of sources to learn SQL from for usage in Rails apps (PostgreSQL mainly I guess?).

like image 353
Jakub Kopyś Avatar asked Mar 14 '23 00:03

Jakub Kopyś


1 Answers

You can use the keyword "or" in your SQL queries:

Guide.where('description LIKE :search OR name LIKE :search', search: "%#{params[:search]}%")

About the doc, you can try this website which seems to offer interactive courses about SQL in general: http://www.sqlcourse.com/

This website is about PostgreSQL: http://www.tutorialspoint.com/postgresql/postgresql_overview.htm

Also, ILIKE might be usefull in this case (it is case-insensitive).

like image 133
MrYoshiji Avatar answered Apr 01 '23 01:04

MrYoshiji