From 4d208cca7be9eecff35c9c6e8707eb63dace52f5 Mon Sep 17 00:00:00 2001 From: Jane Sandberg Date: Thu, 17 Mar 2022 20:27:45 -0700 Subject: [PATCH] Don't duplicate employers in the employer listings, helps with #63 --- app/models/employer.rb | 2 +- spec/models/employer_spec.rb | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) 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