数据库调用问题

时间:2015-06-07 18:43:23

标签: ruby-on-rails ruby pg

我一直试图提出一个数据库调用,它会从问卷调查表的选择列中为我提供所有内容。我能够迭代并自己列出选项,但它们有自己的索引值

代码@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(&#39;选择&#39;)。全部返回

     [<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">]

1 个答案:

答案 0 :(得分:0)

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

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

所以,你的第一步应该是将一个包含选择的字符串除以ehm选择数组。这可以通过

来完成
  • 数据库重新设计(从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。此步骤根据您之前选择的选项而有所不同。