This project is archived and is in readonly mode.
[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
-
Will Bryant April 9th, 2009 @ 03:27 AM
Use :conditions => {:authors => {:name => 'BAR'}}. AFAIK :conditions => {'authors.name' => 'BAR'} has never been officially supported, though it may have worked previously.
-
Joel Chippindale June 16th, 2009 @ 10:03 AM
- Tag set to activerecord, patch
The dot notation continues to work and is included in the finder unit tests on master.
The attached fix resolves this problem with the table_names from dot notation keys leaking into subsequent conditions.
-
Joel Chippindale June 17th, 2009 @ 11:13 AM
- Title changed from sanitize_sql_hash_for_conditions uses incorrect table_name to [PATCH] sanitize_sql_hash_for_conditions uses incorrect table_name
-
Joel Chippindale June 20th, 2009 @ 05:50 AM
Judging by responses I have had to this ticket this does not seem to be a well known feature however it has been around for a while and is clearly included in the rails guides
-
Pratik June 24th, 2009 @ 09:30 AM
- State changed from new to incomplete
Hey,
Your patch is missing tests. Could you please add a test ?
Thanks.
-
Joel Chippindale June 26th, 2009 @ 12:22 PM
- Assigned user set to Pratik
Here's a new version with a test.
-
Pratik July 29th, 2009 @ 07:17 PM
Hey Joel,
Is it possible to write the test without stubbing ? Also the test should be using :joins instead of :include - as per the guide and the recommended way.
Thanks.
-
Joel Chippindale August 4th, 2009 @ 01:00 PM
New patch attached using :joins instead of :include and without the stubbing.
Be aware that (since removing the stubbing) the existing code will pass this test if your installation of ruby happens to process the 'title' hash key before the 'authors.name' key. The patched code will pass the test independently of the order in which the hash keys are processed.
-
Santiago Pastorino February 2nd, 2011 @ 05:03 PM
- State changed from incomplete to open
- Importance changed from to
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino February 2nd, 2011 @ 05:03 PM
- State changed from open to stale
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>