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)

4

2 回答 2

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 %>
于 2013-11-11T07:58:04.650 回答
1

answer.find_by_sql 是错误的!在 [] 之前使用空格或()

<% @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>

而且你不需要使用@slno- 使用each_with_index

重构代码:

<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>
于 2013-11-11T07:51:44.507 回答