これを少しきれいにするにはどうしたらいいですか?私は、各case
文で私が使用しているすべての繰り返しa.question == f.question
の比較などの不要なコードを取り除きたい:ケースステートメント内の繰り返し条件を回避する方法
def notifications_lookup(filters, answers)
filters.flat_map do |f|
answers.select do |a|
case a.question.question_type
when "image"
a.question == f.question && a.answer_image.image_id == f.answer.to_i
when "single"
a.question == f.question && a.choice_answer.choice_id == f.answer.to_i
when "list"
a.question == f.question && a.choice_answer.choice_id == f.answer.to_i
when "multiple"
a.question == f.question && !(f.answer.split(",").map(&:to_i) & a.answer_multiple.choices.map(&:id)).empty?
when "rating"
results = verify_condition(f, a)
a.question == f.question && results.any?
else
a.question == f.question
end
end
end
end
def verify_condition(filter, a)
a.answer_raitings.map do |r|
r.raiting == filter.raiting && filter.answer.split(",").map(&:to_i).include?(r.response)
end
end
このような状況は、通常、ロジックがうまく考えられていない場合に発生します。 –