#538 √ invalid
...Paul

has_many :through generates bad SQL during include?

Reported by ...Paul | July 2nd, 2008 @ 07:14 PM | in 2.x

If I have a class that has_many of another class :through a join table, I find I get a SQL error from the .include?(obj) method. This doesn't happen if the related data is already loaded.

class Channel < ActiveRecord::Base

has_many :users, :through => :subscriptions

end

u = User.find(:first)

c = Channel.find(:first)

c.users.include?(u)

Mysql::Error: Column 'id' in where clause is ambiguous: SELECT `users`.id FROM `users1` INNER JOIN subscriptions ON users.id = subscriptions.user_id WHERE (id = 19) AND ((`subscriptions`.channel_id = 54)) LIMIT 1

>c.users

>c.users.include?(u)

true

This code worked fine before upgrading to 2.1. I can't seem to find where this bare "id" in the WHERE clause could be coming from...

Comments and changes to this ticket

  • ...Paul

    ...Paul July 2nd, 2008 @ 07:41 PM

    The problem exists somewhere in the heirarchy of the "exists?" method in active_record/base.rb (or, in active_record/associations/association_collection.rb, where exists? is called from the include? method, passing only the record to be searched for).

    I found if I kludged the include? method so the exists call looked like this:

    exists?(["#{quoted_table_name}.#{primary_key} = ?", record.id])

    Things worked for me, at least in this case. I'm fairly certain that's not a really a good patch, but it confirmed my suspicions that something in that heirarchy isn't properly using the table name space in the query. :P

  • Tarmo Tänav

    Tarmo Tänav September 18th, 2008 @ 07:45 AM

    • → State changed from “new” to “invalid”

    I'm unable to reproduce it, looking at sanitize_sql_hash_for_conditions() which exists?() calls indirectly through expand_id_conditions() it seems that everything should get properly prefixed with a table name.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins

People watching this ticket