Ruby on rails 数据库调用问题

Ruby on rails 数据库调用问题,ruby-on-rails,ruby,pg,Ruby On Rails,Ruby,Pg,我一直在尝试提出一个数据库调用,它将为我提供从问卷表的“选择”列中的所有信息。我能够迭代并列出选项本身,但它们有自己的索引值 代码@choices=问卷。选择(:choices)。所有[0]只给我第一个 我希望数据库调用为@choices=inventory.select(:choices).all[I],允许我访问所有这些内容。我试过我能想到的循环。如果有人在那里,请帮助我。我为此奋斗了一整天。谢谢 问卷控制员 class QuestionnairesController < Appli

我一直在尝试提出一个数据库调用,它将为我提供从问卷表的“选择”列中的所有信息。我能够迭代并列出选项本身,但它们有自己的索引值

代码
@choices=问卷。选择(:choices)。所有[0]
只给我第一个

我希望数据库调用为
@choices=inventory.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
问卷。选择(“选择”)。所有返回

     [<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">]
[,,
,
,
]
首先要考虑的是,为什么要将所有可能的选择存储为单个字符串?您如何知道第一个选项的文本在哪里结束,第二个选项从哪里开始

所以,您的第一步应该是划分一个包含字符串to、ehm和选项数组的选项。这可以通过以下方式完成:

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


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

我被告知无法在数据库中存储数组。我已经设置了一个选项数组,迭代的最佳方式是什么?您可以手动操作(使用类似于
的选项。每个选项都执行
)并为每个选项呈现一个输入选项,或者您可以使用SmartRails帮助程序:请参阅
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: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'
Questionnaire.first.choices.split(',')
# => ["1982 Michael Jackson Bille Jean", "1984 Madonna Like a virgin", "1981 The Buggles Video Killed The Radio Star"]