Ticket #5971 - Patch to fix sanitize_sql for associations.

There’s a very minor bug in the associations when generating conditions. It uses @reflection.active_record.sanitize_sql which results in Hash type conditions getting the table name of the model that has the association instead of on the association table.

For example, something like:

class Person < ActiveRecord::Base
has_one :address, :conditions => { :disabled => false }
has_many :shipments, :through => :address

if you do:

p = Person.find(1)

the sql it generates to fetch the shipments will include in it’s conditions:

people.disabled = ‘0’

when that should be

addresses.disabled = ‘0’

All we need to do to fix that is have each call to sanitize_sql be done with the class the conditions are being called on as the receiver.


And it’s solved:

Here’s the patch link. I’d like to get this in so I don’t have to write a monkey patch to do it since it’s considerably more code than the 3 line fix to core.


Let me know if there’s anything I can do to help this through.

-Martin Emde