0

In my quiz i have created a questions table with numbers it is used for whether the question is answered or not. if it answered the visited column in answer table goes to 1 or else 0 as

+----+--------+--------------+---------+---------------+------------+-------+---------+
| id | answer | questions_id | user_id | exam_group_id | modules_id | marks | visited |
+----+--------+--------------+---------+---------------+------------+-------+---------+
|  1 | ans2   |            8 |       3 |             1 |          1 |     0 |       1 |
|  2 | NULL   |            9 |       3 |             1 |          1 |     0 |       0 |
|  3 | NULL   |            6 |       3 |             1 |          1 |     0 |       0 |
|  4 | ans1   |            5 |       3 |             1 |          2 |     1 |       1 |
|  5 | NULL   |            4 |       3 |             1 |          2 |     0 |       0 |
|  6 | NULL   |            3 |       3 |             1 |          2 |     0 |       0 |
+----+--------+--------------+---------+---------------+------------+-------+---------+

and i checked the questions based on visited in my view page as like

<% @slno = 0 %>
  <ul class="student_list">
    <% @questions.each do |s| %>
    <% @slno = @slno + 1 %>
      <% if ((Answer.find_by_sql["SELECT visited from answers where questions_id=#{s.id}"]) == 1) %>
        <li class="student_names">
          <a href="#" id="<%=s.id%>"  class="student-link" > <%= @slno %></a>
        </li>
        <% else %>
        <li class="student_names2">
          <a href="#" id="<%=s.id%>"  class="student-link2" > <%= @slno %></a>
        </li>
      <% end %>
    <% end %>
  </ul>

but it gives the error as wrong number of arguments (0 for 1)

1
  • what line do you get the error? Commented Nov 11, 2013 at 7:43

2 Answers 2

1

Answer.find_by_sql is wrong! Use space before [] or ()

<% @slno = 0 %>
<ul class="student_list">
  <% @questions.each do |s| %>
  <% @slno = @slno + 1 %>
    <% if ((Answer.find_by_sql(["SELECT visited from answers where questions_id=#{s.id}"])) == 1) %>
      <li class="student_names">
        <a href="#" id="<%=s.id%>"  class="student-link" > <%= @slno %></a>
      </li>
      <% else %>
      <li class="student_names2">
        <a href="#" id="<%=s.id%>"  class="student-link2" > <%= @slno %></a>
      </li>
    <% end %>
  <% end %>
</ul>

And you don't need to use @slno - use each_with_index

refactored code:

<ul class="student_list">
  <% @questions.each_with_index do |s, index| %>
    <% if ((Answer.find_by_sql(["SELECT visited from answers where questions_id=#{s.id}"])) == 1) %>
      <li class="student_names">
        <a href="#" id="<%=s.id%>"  class="student-link" > <%= index %></a>
      </li>
    <% else %>
      <li class="student_names2">
        <a href="#" id="<%=s.id%>"  class="student-link2" > <%= index %></a>
      </li>
    <% end %>
  <% end %>
</ul>
Sign up to request clarification or add additional context in comments.

4 Comments

please pay attention on @Supremacy answer. It suggests a good improvement - to use method where instead of raw SQL
i tried it does not works when we use index it returns from 0 right
possible problem is in incorrect column name. your variant should be Answer.where(questions_id: s.id).count == 1 (you have questions_id not question_id) or even simpler Answer.where(questions_id: s.id).any?
additional note: maybe I did not catch your if-conditions. it confuses me because you select column visited from table where questions_id = :id and compare the result with 1. I do not understand how it works and I am not sure if it really works. In my console I get Array of objects and if I start to compare it with 1 I always get false. Please review/recheck your code.
1

You need to wrap your find_by_sql in () like so:

Answer.find_by_sql(["SELECT visited from answers where questions_id=#{s.id}"])

you also don't need to write your own SQL in this instance, instead you can simply do this:

<% if Answer.where(questions_id: s.id).count == 1 %>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.