Created
January 14, 2011 20:15
-
-
Save metaskills/780158 to your computer and use it in GitHub Desktop.
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
assert_equal 1, posts(:welcome).tags.count | |
# Arel 1 | |
# ------ | |
SELECT TOP (1) [posts].* FROM [posts] WHERE ([posts].[id] = 1) | |
SELECT COUNT(DISTINCT [tags].[id]) AS count_id | |
FROM [tags] | |
LEFT OUTER JOIN [taggings] ON [taggings].[tag_id] = [tags].[id] | |
INNER JOIN [taggings] AS [taggings_crltd] ON [tags].id = [taggings_crltd].tag_id | |
WHERE (([taggings].taggable_id = 1) AND ([taggings].taggable_type = N'Post')) | |
# Arel 2 | |
# ------ | |
# core.source | |
# <Arel::Nodes::JoinSource:0x1023a0c20 | |
# @left=#<Arel::Table:0x1023c1b50 | |
# @aliases=[], @columns=nil, @engine=ActiveRecord::Base, | |
# @primary_key=nil, @table_alias=nil, @name="tags" | |
# >, | |
# @right=[ | |
# #<Arel::Nodes::OuterJoin:0x1023a0cc0 | |
# @left=#<Arel::Table:0x1023a16e8 | |
# @aliases=[], @columns=nil, @engine=ActiveRecord::Base, | |
# @primary_key=nil, @table_alias=nil, @name="taggings" | |
# >, | |
# @right=#<Arel::Nodes::On:0x1023a0d10 | |
# @expr=#<Arel::Nodes::Equality:0x1023a0ea0 | |
# @left=#<struct Arel::Attributes::Attribute | |
# relation=#<Arel::Table:0x1023a14e0 | |
# @aliases=[], @columns=nil, @engine=ActiveRecord::Base, | |
# @primary_key=nil, @table_alias=nil, @name="taggings" | |
# >, | |
# name="tag_id" | |
# >, | |
# @right=#<struct Arel::Attributes::Attribute | |
# relation=#<Arel::Table:0x1023a1328 | |
# @aliases=[], @columns=nil, @engine=ActiveRecord::Base, | |
# @primary_key=nil, @table_alias=nil, @name="tags" | |
# >, | |
# name="id" | |
# > | |
# > | |
# > | |
# >, <-- Arel::Nodes::OuterJoin | |
# #<Arel::Nodes::StringJoin:0x1023a0d88 | |
# @left="INNER JOIN [taggings] ON [tags].id = [taggings].tag_id", | |
# @right=nil | |
# > | |
# ] | |
# > | |
# Find rails through relationship joins that need to be correlated. Would be something like. | |
Arel::Nodes::JoinSource === core.source | |
Arel::Nodes::OuterJoin === core.source.right.first | |
Arel::Nodes::StringJoin === core.source.right.second | |
Arel::Nodes::OuterJoin - LEFT OUTER JOIN [taggings] ON [taggings].[tag_id] = [tags].[id] | |
Arel::Nodes::StringJoin - LEFT OUTER JOIN [taggings] AS [taggings_2] ON [taggings_2].[tag_id] = [tags].[id] | |
core.source.right.many? | |
j1 = core.source.right.first | |
j2 = core.source.right.second | |
j1tn = j1.left.name # => "taggings" | |
j2tn = j2.left.match(/JOIN \[(.*)\].*ON/).try(:[],1) # => "taggings" | |
visit core.source.right.first # => "LEFT OUTER JOIN [taggings] ON [taggings].[tag_id] = [tags].[id]" | |
visit core.source.right.second # => "INNER JOIN [taggings] ON [tags].id = [taggings].tag_id" | |
j1.left.table_alias = 'taggings_2' | |
visit(j1) # => "LEFT OUTER JOIN [taggings] [taggings_2] ON [taggings].[tag_id] = [tags].[id]" | |
j1.right.expr.left.relation.table_alias = 'taggings_2' | |
visit(j1) # => "LEFT OUTER JOIN [taggings] [taggings_2] ON [taggings_2].[tag_id] = [tags].[id]" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment