public
Created

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
 
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]"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.