Skip to content

Commit

Permalink
Handle when table is already being joined
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew McGarvey authored and matthewmcgarvey committed Oct 14, 2020
1 parent 7d6ba29 commit b68de72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions spec/query_associations_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,19 @@ describe "Query associations" do

result.should eq(line_item_product)
end

it "can query with potentially unnecessary joins again" do
item = LineItemBox.create
product = ProductBox.create
line_item_product = LineItemProductBox.create &.line_item_id(item.id).product_id(product.id)

line_item_query = LineItemQuery.new
.id(item.id)
.where_line_items_products(LineItemProductQuery.new.id(line_item_product.id))
result = ProductQuery.new
.where_line_items(line_item_query)
.find(product.id)

result.should eq(product)
end
end
2 changes: 1 addition & 1 deletion src/avram/query_builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class Avram::QueryBuilder
end

def join(join_clause : Avram::Join::SqlClause)
if join_clause.to != table
if join_clause.to != table && @joins.none? { |join| join.to == join_clause.to }
@joins << join_clause
end
self
Expand Down

0 comments on commit b68de72

Please sign in to comment.