diff --git a/app/models/employer.rb b/app/models/employer.rb index c46efe4..4d1d7d0 100644 --- a/app/models/employer.rb +++ b/app/models/employer.rb @@ -9,7 +9,7 @@ class Employer < ApplicationRecord default_scope { order(name: :asc) } - scope :with_published_jobs, -> { joins(:jobs).where(jobs: { published: true }) } + scope :with_published_jobs, -> { joins(:jobs).where(jobs: { published: true }).distinct } validates :name, presence: true diff --git a/spec/models/employer_spec.rb b/spec/models/employer_spec.rb index 300fc71..2a79cee 100644 --- a/spec/models/employer_spec.rb +++ b/spec/models/employer_spec.rb @@ -1,5 +1,9 @@ require 'rails_helper' RSpec.describe Employer, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + it 'uses distinct when listing employers' do + employer = described_class.create name: 'The Library' + (1..5).each { |job| Job.create employer: employer, title: job, description: job, job_type: job, published: true } + expect(described_class.with_published_jobs.count).to eq(1) + end end