I have a users
table and a tasks
table with a model with the following association:
class Task < ActiveRecord::Base
belongs_to :owner, class_name: User
end
I'm trying to build a query that will search the task
's description
and its owner
's name as such:
includes(:owner).where("LOWER(tasks.description) LIKE ? OR LOWER(owner.name) LIKE ?", "%#{q.downcase}%", "%#{q.downcase}%")
...but I don't know how to properly query the users table for the task owners. In place of owner.name
, I've tried users.name
, tasks.users.name
, and probably a few other things, all to no avail. How can I do this?
Note: I do not want to add a gem for this. I'm looking for a solution that is native to rails.
EDIT:
The foreign key as it exists in my schema.rb
add_foreign_key "tasks", "users", :name => "tasks_owner_id_fk", :column => "owner_id", :dependent => :nullify
SECOND EDIT:
I also need a solution that will return an AREL. I can get this to work by returning an array of objects, but I need to add other query methods to the result, so it has to be AREL.