gpt4 book ai didi

ruby-on-rails - 关系数据库(外键)

转载 作者:行者123 更新时间:2023-11-29 13:26:44 25 4
gpt4 key购买 nike

我的应用程序中有一个类别表,我希望我的所有问题都与问题分配到的类别相匹配。问题是每个类别我有四个按钮。每个问题都有一个应从类别表中引用的 category_id。第一个问题很好,因为它实际上属于音乐类别,但是当我按电视类别时,它会呈现属于音乐类别的第二个问题,但它应该呈现它应该属于的问卷表中的第三个问题到电视类别。这是一个外键问题,我正在寻找可以解决它的可能解决方案。

问卷迁移

class CreateQuestionnaire < ActiveRecord::Migration
def change
create_table :questionnaire do |t|
t.string :question
t.string :choices
t.string :correct_answer
t.integer :category_id
t.foreign_key :categories
t.timestamps null: false
end
end
end

类别表种子

Category.create({name:'Music'})
Category.create({name:'TV'})
Category.create({name:'Movies'})
Category.create({name:'Games'})

问卷表种子

Questionnaire.create({question: "In what 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 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: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})

类别 Controller

class CategoriesController < ApplicationController

before_action :require_player, only: [:index]

def index
@categories=Category.all
###It shows a list of all the categories
# binding.pry



render :index
end

end

类别#索引

<h2>Categories </h2>



<% @categories.each do |category| %>
<form action = '/categories/<%=category.id %>/questionnaires/' method = 'GET'>

<ul>

<button type='submit' class="btn btn-default btn-block" style='vertical-align: middle; margin:0px;'><%=category.name%></button>

<br>
</ul>

</form>
<%end%>

问卷 Controller

class QuestionnairesController < ApplicationController

def index

@question = Questionnaire.find(params[:category_id])
@category = Category.find(params[:category_id])
@videos = VideoClue.find(params[:category_id])
###This finds all the questions from the question table by their category_id. Whenever I select a category, it matches the question related to the category

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

问卷#显示

 <h1><%=@question.question%></h1>

最佳答案

首先,表格是复数名称。在您的情况下:问卷s

因为一个questionare belongs_to a category 和一个category has_many questionaires 你只需进入迁移并去寻找

t.references :category
add_index :questionares, :category_id

然后它就可以与 rails 一起正常工作了。

-@categories.each do |category|
.category
%h2=category.name
-category.questions.each do |question|
.question
%h3=question.question
-question.answers.each_with_index do |answer, index|
%span.answer
Number: ##{index}
%br
=answer.answer

顺便说一句:我会给你一个稍微好一点的模型

class Category
has_many :questions

class Question
has_many :answers
has_one :correct_answer #save as correct_answer_id
attr_accessor :given_answer_id
def correct_answered?
given_answer_id == correct_answer_id
end

class Answer
belongs_to :question

关于ruby-on-rails - 关系数据库(外键),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32516749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com