lundi 14 septembre 2015

Get random child record without accessing individual parent record - Rails 4

I am creating a quiz like website.

I have categories, questions and choices as tables.

What i want is, where i list my categories, when i click on category, i want it to take me to the categories/1/questions/<random question> path.

the relationships are set up

here is what i have so far.

index.erb.html (of categories)

    <ul class="categories-list">
      <% @categories.each do |category| %>
        <% if category.header %>
        <% @count = @count + 1 %>
          <li class="panel header" id="cat<%= @count %>">
            <a href="#"><%= category.title %></a>
            <small>781 of 781 questions remaining</small>
          </li>
        <% else %>
          <li class="panel sub hidden subcat<%= @count %>" >
            <%= link_to category.title, category_question_path(category,category.questions.limit(1).order("RAND()")) %>
            <small>781 of 781 questions remaining</small>
          </li>
        <% end %>

      <% end %>
    </ul>

categories-controller.rb

  def index
    @count = 0
    @categories = Category.order(:tag)

  end

Aucun commentaire:

Enregistrer un commentaire