Skip to content

Commit

Permalink
Only ever include an artist once in #listed scope
Browse files Browse the repository at this point in the history
This fixes a bug in production where we were showing artists multiple
times if they had multiple published albums.
  • Loading branch information
chrislo committed Nov 16, 2023
1 parent f071c12 commit 635926b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/artist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Artist < ApplicationRecord

validates :name, presence: true

scope :listed, -> { where.associated(:albums).where('albums.published': true) }
scope :listed, -> { where.associated(:albums).where('albums.published': true).distinct }

after_update :transcode_albums, if: :metadata_changed?

Expand Down
8 changes: 8 additions & 0 deletions test/models/artist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ class ArtistTest < ActiveSupport::TestCase
assert_equal [listed_artist], Artist.listed
end

test '.listed only returns artist once if they have multiple published albums' do
published_album = create(:album, published: true)
another_published_album = create(:album, published: true)
listed_artist = create(:artist, name: 'Listed', albums: [published_album, another_published_album])

assert_equal [listed_artist], Artist.listed
end

test '#listed?' do
published_album = create(:album, published: true)
unpublished_album = create(:album, published: false)
Expand Down

0 comments on commit 635926b

Please sign in to comment.