Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Active Record Search - Name includes a word

Im trying to pull all records from a Project model that includes in the project_name the word 'Fox'. I can do an active record search and return specific project_names, like 'Brown Fox':

@projects = Project.where("project_name like ?", "Brown Fox")

But if I want to return all the names that INCLUDE 'Fox', this does not work unless the complete project name is 'Fox':

@projects = Project.where("project_name like ?", "Fox")

How do I do a search that returns all the objects with the word 'Fox' in the name?

like image 823
JakeTy Avatar asked Jul 30 '13 20:07

JakeTy


1 Answers

Try using:

variable = "Fox"
Project.where("project_name like ?", "%#{variable}%")
like image 81
Rudy Seidinger Avatar answered Sep 30 '22 20:09

Rudy Seidinger