title: working with hash keys and values
body: <p>I have a model QuizAttempt that is going to check the result of a Quiz. I am looking to loop through each submitted answer and check the question id, if the answer supplied is correct. I'm looking for some guidance please...</p>
  
  <pre><code>class QuizAttempt &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :quiz
  
    validates_presence_of :quiz, :user
    validate :check_result
  
    attr_accessor :questions, :submitted_answers
  
    private
  
    def self.create_for_user_and_answers!(user, answers)
      self.new(:user =&gt; user).tap do |q| 
        q.submitted_answers = answers
        q.questions = []
        answers.each{|k, v| q.questions = q.questions &lt;&lt; Question.find(k.gsub(/[^0-9]/i, '').to_i) }
      end
    end
  
    def check_result
      if submitted_answers
        unless submitted_answers.keys.length == quiz.easy_questions + quiz.moderate_questions + quiz.hard_questions
          self.errors.add(:submitted_answers, "must be provided for each question")
        end
      else
        self.errors.add(:submitted_answers, "must be provided")
      end
  
      return false unless self.errors.empty?
  
      score = 0
      submitted_answers.each do |answer|
        #check the answers and score + 1 if correct
      end
      self.total_questions = submitted_answers.length
      self.passed_questions = score
      self.passed = percentage_score &gt;= quiz.pass_percentage
    end
  
    public
  
    def percentage_score
      (passed_questions / total_questions.to_f * 100).to_i
    end
  
  end
  </code></pre>
  
  <p>The submitted answers are in the form of a (nested?) hash, returned from a form with radio buttons</p>
  
  <pre><code>{"question_1"=&gt;{"answer_3"=&gt;"5"}, "question_2"=&gt;{"answer_2"=&gt;"4"}}
  </code></pre>
  
  <p>But when I loop through them as in the above QuizAttempt model ie. submitted_answers.each do |answer| i get answer ==</p>
  
  <pre><code>["question_1", {"answer_3"=&gt;"5"}]
  </code></pre>
  
  <p>And I want to check these answers based on the question model below</p>
  
  <pre><code>class Question &lt; ActiveRecord::Base
    belongs_to :quiz
  
    validates :question, :presence =&gt; true, :length =&gt; {:minimum =&gt; 3, :maximum =&gt; 254}
    validates :answer_1, :answer_2, :answer_3, :answer_4, :presence =&gt; true, :length =&gt; {:maximum =&gt; 254}
    validates :rank, :presence =&gt; true, :numericality =&gt; { :only_integer =&gt; true, :greater_than =&gt; 0, :less_than =&gt; 4 }
    validate :only_one_answer_correct  
  
    #has boolean values answer_1_correct, answer_2_correct, answer_3_correct, answer_4_correct
  
    def correct_answer_number
      (1..4).each{|i| return i if send("answer_#{i}_correct")}
    end
  end
  </code></pre>