Created
January 26, 2012 18:31
-
-
Save bclennox/1684228 to your computer and use it in GitHub Desktop.
Rails 3.1.* bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Article < ActiveRecord::Base | |
has_many :comments | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative '../test_helper' | |
class ArticleTest < ActiveSupport::TestCase | |
def check_table_count(count, relation) | |
assert_equal count, relation.instance_eval { tables_in_string(to_sql).size } | |
end | |
def test_plain | |
check_table_count(1, Article.where(:title => 'foo.bar.baz')) | |
end | |
def test_includes | |
check_table_count(1, Article.includes(:comments)) | |
end | |
def test_includes_with_dots | |
check_table_count(1, Article.where(:title => 'foo.bar.baz').includes(:comments)) | |
end | |
def test_includes_with_conditions_on_join | |
check_table_count(2, Article.where('comments.approved = 1').includes(:comments)) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Comment < ActiveRecord::Base | |
belongs_to :article | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby-1.9.2-p290 :004 > rel1 = Article.where(:title => 'foo').includes(:comments) | |
Article Load (0.4ms) SELECT "articles".* FROM "articles" WHERE "articles"."title" = 'foo' | |
=> [] | |
ruby-1.9.2-p290 :005 > rel1.instance_eval { tables_in_string(to_sql) } | |
=> ["articles"] | |
ruby-1.9.2-p290 :006 > rel2 = Article.where(:title => 'foo.bar').includes(:comments) | |
SQL (0.3ms) SELECT "articles"."id" AS t0_r0, "articles"."title" AS t0_r1, "comments"."id" AS t1_r0, "comments"."article_id" AS t1_r1 FROM "articles" LEFT OUTER JOIN "comments" ON "comments"."article_id" = "articles"."id" WHERE "articles"."title" = 'foo.bar' | |
=> [] | |
ruby-1.9.2-p290 :007 > rel2.instance_eval { tables_in_string(to_sql) } | |
=> ["articles", "foo"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateArticlesAndComments < ActiveRecord::Migration | |
def change | |
create_table :articles | |
create_table :comments do |t| | |
t.belongs_to :article | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb | |
index 30f1824..d7335f3 100644 | |
--- a/activerecord/lib/active_record/relation.rb | |
+++ b/activerecord/lib/active_record/relation.rb | |
@@ -528,7 +528,13 @@ module ActiveRecord | |
return [] if string.blank? | |
# always convert table names to downcase as in Oracle quoted table names are in uppercase | |
# ignore raw_sql_ that is used by Oracle adapter as alias for limit/offset subqueries | |
- string.scan(/([a-zA-Z_][.\w]+).?\./).flatten.map{ |s| s.downcase }.uniq - ['raw_sql_'] | |
+ candidates = string.scan(/([a-zA-Z_][.\w]+).?\./).flatten.map{ |s| s.downcase }.uniq - ['raw_sql_'] | |
+ candidates.reject do |t| | |
+ s = string.partition(t).first | |
+ s.chop! if s.last =~ /['"]/ | |
+ s.reverse! | |
+ s =~ /^\s*=/ | |
+ end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment