Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous clause in Rails

I'm doing a polymorphic join like so :

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

And it's returning as so :

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

Anyone know how to make this un ambigous? :)

like image 652
Trip Avatar asked May 14 '12 19:05

Trip


2 Answers

It doesn't like the name LIKE section.

It looks like there is a column named name in each of the tables. Preface that specific name with the name of the table you want that value to be from.

This 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've found in general, the best way to resolve these issues is to view the sql generated and determine where the problem is from that. You can find the sql in the logs.

like image 100
Kevin Bedell Avatar answered Sep 27 '22 15:09

Kevin Bedell


name must be a property (and column) in both jobs and whatever the other object (table) is therefore it needs to be fully qualified.

change

"name like..." 

to

"tablename.name like..." 

just like you are qualifying

"jobs.name"
like image 30
Gratzy Avatar answered Sep 27 '22 16:09

Gratzy