Q:

StatementInvalid Rails Query

I've got the following query that works:
jobs = current_location.jobs.includes(:customer).all.where(complete: complete)

However, when I add a where clause to query the first name of the customer table, I get an error. 
jobs = current_location.jobs.includes(:customer).all.where(complete: complete).where("customers.fist_name = ?", "Bob")

Here is the error:
PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "customers"
LINE 1: ...bs"."complete" = $2 AND "jobs"."status" = $3 AND (customers....
                                                         ^
: SELECT  "jobs".* FROM "jobs" INNER JOIN "jobs_users" ON "jobs"."id" = "jobs_users"."job_id" WHERE "jobs_users"."user_id" = $1 AND "jobs"."complete" = $2 AND "jobs"."status" = $3 AND (customers.last_name = 'Bob') ORDER BY "jobs"."start" DESC LIMIT $4 OFFSET $5

The current_location method:
def current_location
    return current_user.locations.find_by(id: cookies[:current_location])
end

Location Model 
has_many :jobs
has_and_belongs_to_many :customers

Job Model
belongs_to :location
belongs_to :customer

Customer Model
has_many :jobs
has_and_belongs_to_many :locations

How can I fix this issue?

A:

includes will only join the table if you set a reference to the association.
When using includes you ensure a reference to the association in 2 fashions:

You can use the references method this will join the table whether or not there are any query conditions (If you MUST use raw SQL as shown in your question then this is the method you would need to use)  e.g.
current_location.jobs
  .includes(:customer)
  .references(:customer)

Or you can use the hash finder version of where: (Please note that when using an associative reference in the where clause you must reference the table name, in this case customers and not the association name customer)
current_location.jobs
  .includes(:customer)
  .where(customers: {first_name: "Bob" }) 

Both of these will eager load the customer for the jobs referenced. 
The first option (references) will OUTER JOIN the customers table so that all the jobs are loaded even if they have no customers as long as no query conditions reference the customers table. 
The second option (using where) will OUTER JOIN the customers table but given the query parameter against the customers table it will act very much like an INNER JOIN.
If you only need to search the jobs based on customer information then joins is a better choice as this will create an INNER JOIN with the customers table but will not try to load any of the customer data in the query e.g. 
current_location.jobs.joins(:customer).where(customers: {first_name: "Bob" }) 

joins will always include the associated table regardless of a reference in the query.
Sidenote: the all in both your queries is completely unnecessary 

