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:
parent
0a1b434607
commit
9bcdc90cfd
2 changed files with 23 additions and 2 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue