An ambiguous sentence in Rails

I am making a polymorphic compound as follows:

Object.joins(:customer).includes("jobs.name").merge(@customer.children.scoped).where("name LIKE :name OR job_number LIKE :name", {:name => "JOB" } ) 

And he returns like this:

 Mysql2::Error: Column 'name' in where clause is ambiguous 

Does anyone know how to make this unambiguous? :)

+6
source share
2 answers

I do not like the name LIKE section.

It looks like each table has a column called name . Imagine a specific name with the name of the table from which you want this value to be.

It looks like it should be either

 .where("jobs.name LIKE :name OR job_number LIKE :name", {:name => "JOB" } ) 

or

 .where("customers.name LIKE :name OR job_number LIKE :name", {:name => "JOB" } ) 

I found in general, the best way to solve these problems is to look at the sql created and determine where the problem is. You can find sql in the logs.

+7
source

name must be a property (and a column) in both tasks, and, nevertheless, any other object (table) must be fully qualified.

change

 "name like..." 

to

 "tablename.name like..." 

just like you qualify

 "jobs.name" 
+3
source

Source: https://habr.com/ru/post/915716/


All Articles