At the very least the question_choices.all(:select => 'id').map {|x| x.id}
component can be rewritten, as ActiveRecord provides a method for this question_choice_ids
.
You can also just do find
instead of find_by_id
. I know that find
will raise an exception if nothing is found, but so will calling question_choices
on nil
in your example, anyway:
Question.find(question_id).question_choice_ids.include?(user_choice_id)
# or
# Rails 2 (will run 2 queries unless you use :include)
Question.find(question_id).question_choices.first(:conditions => {:id => user_choice_id})
# Rails 3 (will only run 1 query)
Question.find(question_id).question_choices.where(:id => user_choice_id).first