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