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?).
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With