This project is archived and is in readonly mode.

#2462 ✓stale
ekolve

[PATCH] sanitize_sql_hash_for_conditions uses incorrect table_name

Reported by ekolve | April 8th, 2009 @ 09:33 PM | in 3.x

If you add the following test to activerecord/test/cases/finder_test.rb


  def test_find_with_include_and_conditions_with_dot
    Post.find(:all,
              :include => [ :authors ],
              :conditions => {
                :title => 'FOO',
                'authors.name' => 'BAR'
               }
             )
  end

It will blow up with the error:


ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'authors.title' in 'where clause': SELECT `posts`.`id` AS t0_r0, `posts`.`author_id` AS t0_r1, `posts`.`title` AS t0_r2, `posts`.`body` AS t0_r3, `posts`.`type` AS t0_r4, `posts`.`comments_count` AS t0_r5, `posts`.`taggings_count` AS t0_r6, `authors`.`id` AS t1_r0, `authors`.`name` AS t1_r1, `authors`.`author_address_id` AS t1_r2, `authors`.`author_address_extra_id` AS t1_r3 FROM `posts`  LEFT OUTER JOIN `categorizations` ON (`posts`.`id` = `categorizations`.`category_id`)  LEFT OUTER JOIN `authors` ON (`authors`.`id` = `categorizations`.`author_id`) WHERE (`authors`.`name` = 'BAR' AND `authors`.`title` = 'FOO') 

I traced this back to the method: activerecord/lib/active_record/base.rb#sanitize_sql_hash_for_conditions

If a condition defines a column and includes a '.', the table name will be used for any subsequent columns that do not contain a '.' in them when it should be using quoted_table_name.

Comments and changes to this ticket

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages