I have a piece of code that checks that a survey response picked by user to a survey question is in fact one of valid choices:
Question.find_by_id(question_id).question_choices.all(:select => 'id').map {|x| x.id}.include?(user_choice_id)
Is there an easier way?
Thanks!
At the very least the
question_choices.all(:select => 'id').map {|x| x.id}component can be rewritten, as ActiveRecord provides a method for thisquestion_choice_ids.You can also just do
findinstead offind_by_id. I know thatfindwill raise an exception if nothing is found, but so will callingquestion_choicesonnilin your example, anyway: