关系数据库(外键)

Relational Database (Foreign Keys)

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

问卷迁移

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

类别Table种子

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

问卷Table种子

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})

类别控制器

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

问卷控制器

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