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.
This commit is contained in:
cmrd Senya 2017-08-09 20:09:29 +03:00
parent 0a1b434607
commit 9bcdc90cfd
No known key found for this signature in database
GPG key ID: 5FCC5BA680E67BFE
2 changed files with 23 additions and 2 deletions

View file

@ -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) {

View file

@ -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