Search code examples
ruby-on-railsrubypg

Issue with database call


I have been trying to come up with a database call that would give me everything from the choices column from the questionnaires table. I was able to iterate and list the choices themselves but they have their own index value

The code @choices = Questionnaire.select(:choices).all[0] gives me just the first one

I want the database call to be @choices = Questionnaire.select(:choices).all[i] that would allow me to access all of them regardless. I have tried loops that I can think of. If anyone out there are available please help me out. I have struggled all day on this. Thanks!

Questionnaire Controller

class QuestionnairesController < ApplicationController

  def index

    @questions = Questionnaire.find(params[:category_id])
    #params[:category_id]= <%=category.id%>
    @category = Category.find(params[:category_id])
    @videos = VideoClue.find(params[:category_id])

    render :show
    ###render :show Renders Html page
  end

  def choose_answer

    @questions = Questionnaire.find(params[:id])
    @choices = Questionnaire.select(:choices).all

    render :choose_answer
  end
end

end

Choose_answer.html

<h1>Congrats You Hit The Choices Page!</h1>


<%= semantic_form_for @questions.choices do |c| %>
  <%= c.inputs do |e| %>
    <%= c.input :answer, :as => :check_boxes , :collection => @choices%>
  <% end %>
<% end %>

Seed

Questionnaire.create({question: "In that year did MTV (Music     Television) premiere and what was the first music video the channel    aired?", 
  choices:'1982 Michael Jackson Bille Jean, 1984 Madonna Like a virgin, 1981 The Buggles Video Killed The Radio Star', correct_answer:"1981 The Buggles 'Video Killed The Radio Star' ", category_id:1})

Questionnaire.create({question: "This sitcom featured four girls living under one roof. They attended the same boarding school, ran a shop together and reside in a town called Peekskill." , choices:'Designing Women, The Facts of Life, Girlfriends', correct_answer:'The Facts of Life', category_id: 2})

Questionnaire.create({question: "This martial arts film premiere in 1985 which featured a young man who studies Bruce Lee's techniques while on the search for his master. This was set in New York City." , choices:'The Last Dragon, The Karate Kid, Big Trouble in Little China', correct_answer:'The Last Dragon', category_id: 3})

Questionnaire.create({question:"This game launched in 1991 on Sega Genesis which the player's mission is to collect as many golden rings as possible", choices:'Battletoads, Sonic The Hedgehog, Jewel Master', correct_answer: "Sonic The Hedgehog", category_id:4})

Questionnaire.select('choices').all returns

     [<Questionnaire:0x007fbc2c9fa728
      id: nil,
      choices: "1982 Michael Jackson Bille Jean, 1984 Madonna Like a           virgin, 1981 The Buggles Video Killed The Radio Star">,
    <Questionnaire:0x007fbc2c9fa138 id: nil, choices: "Designing   Women, The Facts of Life, Girlfriends">,
    <Questionnaire:0x007fbc2ca01dc0 id: nil, choices: "The Last  Dragon, The Karate Kid, Big Trouble in Little China">,
    <Questionnaire:0x007fbc2ca00f88 id: nil, choices: "Battletoads, Sonic The Hedgehog, Jewel Master">]

Solution

  • choices: '1982 Michael Jackson Bille Jean, 1984 Madonna Like a virgin,
    1981 The Buggles Video Killed The Radio Star'
    

    The first thing to think about is why do you store all the possible choices as a single string? How can you know where does the text for the first option ends and where does the second one begin?

    So, your first step should be to divide a single choices-containing string to, ehm, array of choices. This can be done by

    • database re-engineering (switching from string to array type);
    • splitting initial string (but in case some of your choices contain comma, you're gonna be in a bad situation)

      Questionnaire.first.choices.split(',')
      # => ["1982 Michael Jackson Bille Jean", "1984 Madonna Like a virgin", "1981 The Buggles Video Killed The Radio Star"]
      

    After doing that you should be able to iterate choices in your view (or rails console). This step differs based on which option you've chosen earlier.