From 9bcdc90cfd06248251639ee3a7dca027eea68488 Mon Sep 17 00:00:00 2001 From: cmrd Senya Date: Wed, 9 Aug 2017 20:09:29 +0300 Subject: [PATCH] Fix Person.in_aspects scope multiple return Fix Person.in_aspects scope to return each person only once when the person is in multiple aspects. --- app/models/person.rb | 4 ++-- spec/models/person_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/models/person.rb b/app/models/person.rb index 3f20a3a84..169a84c18 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -78,8 +78,8 @@ class Person < ActiveRecord::Base #not defensive scope :in_aspects, ->(aspect_ids) { - joins(:contacts => :aspect_memberships). - where(:aspect_memberships => {:aspect_id => aspect_ids}) + joins(contacts: :aspect_memberships) + .where(aspect_memberships: {aspect_id: aspect_ids}).distinct } scope :profile_tagged_with, ->(tag_name) { diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index 8aa2cf57f..7cfe69ab9 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -175,6 +175,27 @@ describe Person, :type => :model do expect(result[1].id).to eq(person1.id) end end + + describe ".in_aspects" do + it "returns person that is in the aspect" do + aspect = FactoryGirl.create(:aspect) + contact = FactoryGirl.create(:contact, user: aspect.user) + aspect.contacts << contact + expect(Person.in_aspects([aspect.id])).to include(contact.person) + end + + it "returns same person in multiple aspects only once" do + user = bob + contact = FactoryGirl.create(:contact, user: user) + ids = Array.new(2) do + aspect = FactoryGirl.create(:aspect, user: user, name: r_str) + aspect.contacts << contact + aspect.id + end + + expect(Person.in_aspects(ids)).to eq([contact.person]) + end + end end describe "delegating" do