Skip to content

Commit

Permalink
Use Base#type_condition in JoinAssociation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Mar 5, 2011
1 parent 7fddb94 commit b7f1b36
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ def join_to(relation)
end

conditions << table[key].eq(foreign_table[foreign_key])

conditions << reflection_conditions(index, table)
conditions << sti_conditions(reflection, table)

if reflection.klass.finder_needs_type_condition?
conditions << reflection.klass.send(:type_condition, table)
end

ands = relation.create_and(conditions.flatten.compact)

Expand Down Expand Up @@ -222,19 +224,6 @@ def reflection_conditions(index, table)
end
end

def sti_conditions(reflection, table)
unless reflection.klass.descends_from_active_record?
sti_column = table[reflection.klass.inheritance_column]
sti_condition = sti_column.eq(reflection.klass.sti_name)
subclasses = reflection.klass.descendants

# TODO: use IN (...), or possibly AR::Base#type_condition
subclasses.inject(sti_condition) { |attr,subclass|
attr.or(sti_column.eq(subclass.sti_name))
}
end
end

end
end
end
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,8 @@ def construct_finder_arel(options = {}, scope = nil)
relation
end

def type_condition
sti_column = arel_table[inheritance_column.to_sym]
def type_condition(table = arel_table)
sti_column = table[inheritance_column.to_sym]
sti_names = ([self] + descendants).map { |model| model.sti_name }

sti_column.in(sti_names)
Expand Down

0 comments on commit b7f1b36

Please sign in to comment.