Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def has_and_belongs_to_many(association_name, scope: nil, resource:, foreign_key
parent_ids = parents.map { |p| p.send(primary_key) }
parent_ids.uniq!
parent_ids.compact!
_scope.call.joins(through).where(through => { fk => parent_ids })
_scope.call.joins(through).where(through => { fk => parent_ids }).distinct
end

assign do |parents, children|
Expand Down
15 changes: 14 additions & 1 deletion spec/integration/rails/finders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def index
let!(:state) { State.create!(name: 'Maine') }
let!(:bio) { Bio.create!(author: author1, picture: 'imgur', description: 'author bio') }
let!(:hobby1) { Hobby.create!(name: 'Fishing', authors: [author1]) }
let!(:hobby2) { Hobby.create!(name: 'Woodworking', authors: [author1]) }
let!(:hobby2) { Hobby.create!(name: 'Woodworking', authors: [author1, author2]) }
let(:house) { House.new(name: 'Cozy', state: state) }
let(:condo) { Condo.new(name: 'Modern', state: state) }
let(:genre) { Genre.create!(name: 'Horror') }
Expand Down Expand Up @@ -271,6 +271,19 @@ def json
expect(hobby).to_not have_key('description')
expect(hobby).to_not have_key('reason')
end

it 'does not duplicate results' do
get :index, params: { include: 'hobbies' }
author1_relationships = json['data'][0]['relationships']
author2_relationships = json['data'][1]['relationships']

author1_hobbies = author1_relationships['hobbies']['data']
author2_hobbies = author2_relationships['hobbies']['data']

expect(json_includes('hobbies').size).to eq(2)
expect(author1_hobbies.size).to eq(2)
expect(author2_hobbies.size).to eq(1)
end
end

context 'sideloading self-referential' do
Expand Down