Say I have a table called books
with a column in it called keyword
.
Say I also have a string called words
.
I want to select all records from books
where the column keyword
is included in words
.
I can do this using LIKE directly in mysql.
I'm trying to translate this into an ActiveRecord query for a rails app if anyone could give me a hand with the syntax.
The tricky part is that I want to find records where the column value is included in my string, not the other way around. Very similar to this question, except I need to translate it to ActiveRecord. SQL - Query to find if a string contains part of the value in Column
Here is a working sql query that I am trying to translate.
SELECT * FROM books WHERE "new science fiction" LIKE CONCAT('%',keyword,'%');
In the above example "new science fiction" is the string words
.
Say I have 3 book
records. book1 has keyword 'science', book2 has keyword 'fiction' and book3 has keyword 'other'. My above query would return the records book1 and book2. Because 'science' and 'fiction' are included in the words
string 'new science fiction'.
My SQL query works but I can't figure out how to do it with a Book.where
statement in ActiveRecord.