Fix extra nil added to assigned investment IDs

When `valuator_group` was `nil`, `[valuator_group&.investment_ids]` is
evaluated to `nil`, and so we were adding an extra element to the array.

We could add a `compact` call to the resulting array, but I find it
easier to convert `nil` to an array using `to_a`.
This commit is contained in:
Javi Martín
2019-09-26 18:56:34 +02:00
parent edc396a8e2
commit 45c6a70d91
2 changed files with 5 additions and 5 deletions

View File

@@ -18,7 +18,7 @@ class Valuator < ApplicationRecord
end
def assigned_investment_ids
investment_ids + [valuator_group&.investment_ids].flatten
investment_ids + valuator_group&.investment_ids.to_a
end
end

View File

@@ -28,8 +28,8 @@ describe Valuator do
investment2.valuators << valuator
assigned_investment_ids = valuator.assigned_investment_ids
expect(assigned_investment_ids).to include investment1.id
expect(assigned_investment_ids).to include investment2.id
expect(assigned_investment_ids).to match_array [investment1.id, investment2.id]
expect(assigned_investment_ids).not_to include investment3.id
end
@@ -45,8 +45,8 @@ describe Valuator do
investment2.valuator_groups << group
assigned_investment_ids = valuator.assigned_investment_ids
expect(assigned_investment_ids).to include investment1.id
expect(assigned_investment_ids).to include investment2.id
expect(assigned_investment_ids).to match_array [investment1.id, investment2.id]
expect(assigned_investment_ids).not_to include investment3.id
end
end