关系数据库(外键)

时间:2015-09-11 06:17:18

标签: ruby-on-rails postgresql foreign-key-relationship

我的应用中有一个类别表,我希望我的所有问题都与问题分配的类别相匹配。问题是每个类别我有四个按钮。每个问题都有一个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})

类别控制器

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>

1 个答案:

答案 0 :(得分:1)

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

因为您只需要迁移select table_1.id, table_1.name, table_1.age, 'edited' as rowstatus from table_1 inner join table_2 on table_1.id=table_2.id and table_1.age<>table_2.age union select id, name, age, 'deleted table_1' as rowstatus from table_1 where id not in (select id from table_2) union select id, name, age, 'deleted from table_2' as rowstatus from table_2 where id not in (select id from table_1) questionare belongs_to a category

category has_many questionaires

然后它就可以正常使用rails。

t.references :category
add_index :questionares, :category_id
顺便说一下:我会给你一个更好的建模

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