Skip to content

Commit

Permalink
Find the table
Browse files Browse the repository at this point in the history
  • Loading branch information
rzane committed Feb 17, 2019
1 parent c2560a6 commit 4ddd304
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions lib/baby_squeel/join_dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ def initialize(relation)
# a list (in order of chaining) of associations and finding
# the respective JoinAssociation at each level.
def find_alias(associations)
table = find_join_association(associations).table
reconstruct_with_type_caster(table, associations)
join_association = find_join_association(associations)

if join_association.table
join_association.table
else
join_association.base_klass.arel_table
end
end

private
Expand All @@ -63,16 +68,6 @@ def reflections_equal?(a, b)
def comparable_reflection(reflection)
[*reflection.parent_reflection].last || reflection
end

# Active Record 5's AliasTracker initializes Arel tables
# with the type_caster belonging to the wrong model.
#
# See: https://github.com/rails/rails/pull/27994
def reconstruct_with_type_caster(table, associations)
return table if ::ActiveRecord::VERSION::MAJOR < 5
type_caster = associations.last._scope.type_caster
::Arel::Table.new(table.name, type_caster: type_caster)
end
end
end
end

0 comments on commit 4ddd304

Please sign in to comment.