I'm using Rails 5. How do I have this model ...
class Book < ApplicationRecord
has_one :author
The author class has an attribute "name". How do I write a query for book objects whose author contains the string "Joe"? I tried this
Book.includes(:author).where(:author => [ "LOWER(name) LIKE :search", :search[Dch => "%joe%"])
but no dice.
Try with:
Book.joins(:author).where('LOWER(authors.name) LIKE ?', '%joe%')
The plural_table_name.column_name LIKE ...
syntax should work.
You can't use hash parameters to call where
in this case, since you're using the LIKE
operator for your query.
Notice I've changed your includes
to joins
since I don't know if you're going to invoke the "child" columns once having the result of your query. joins
is enough to add an INNER JOIN
clause and gain access to the author columns.