数据库调用问题

Issue with database call

我一直在尝试想出一个数据库调用,它可以为我提供调查问卷 table 的选择栏中的所有内容。我能够自己迭代并列出选项,但它们有自己的索引值

代码@choices = Questionnaire.select(:choices).all[0]只给了我第一个

我希望数据库调用是 @choices = Questionnaire.select(:choices).all[i] 这样我就可以访问所有这些数据库。我试过我能想到的循环。如果有人有空,请帮助我。我为此奋斗了一整天。谢谢!

问卷控制器

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

结束

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

种子

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').全部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">]
choices: '1982 Michael Jackson Bille Jean, 1984 Madonna Like a virgin,
1981 The Buggles Video Killed The Radio Star'

首先要考虑的是,为什么要将所有可能的选择存储为一个字符串?您如何知道第一个选项的文本在哪里结束以及第二个选项的文本从哪里开始?

因此,您的第一步应该是将单个包含选项的字符串划分为选项数组。这可以通过

来完成
  • 数据库重新设计(从string类型切换到array类型);
  • 拆分初始字符串(但如果您的某些选择包含逗号,则情况会很糟糕)

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

完成后,您应该能够在您的视图(或 rails 控制台)中迭代 choices。此步骤因您之前选择的选项而异。