Skip to content

Commit

Permalink
Added Quiz model class and association with MultipleChoiceQuestion class
Browse files Browse the repository at this point in the history
  • Loading branch information
sdflem committed Oct 22, 2018
1 parent 8b9cc47 commit 5e0f016
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/models/multiple_choice_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@
# question :string
# created_at :datetime not null
# updated_at :datetime not null
# quiz_id :integer
#
# Indexes
#
# index_multiple_choice_questions_on_quiz_id (quiz_id)
#

class MultipleChoiceQuestion < ApplicationRecord
belongs_to :quiz,
class_name: 'Quiz',
foreign_key: 'quiz_id',
inverse_of: :questions

validates :question, presence: true
validates :answer, presence: true
validates :distractor_1, presence: true
Expand Down
18 changes: 18 additions & 0 deletions app/models/quiz.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# == Schema Information
#
# Table name: quizzes
#
# id :integer not null, primary key
# description :text
# title :string
# created_at :datetime not null
# updated_at :datetime not null
#

class Quiz < ApplicationRecord
has_many :questions,
class_name: 'MultipleChoiceQuestion',
foreign_key: 'quiz_id',
inverse_of: :quiz,
dependent: :destroy
end
10 changes: 10 additions & 0 deletions db/migrate/20181022151903_create_quizzes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateQuizzes < ActiveRecord::Migration[5.2]
def change
create_table :quizzes do |t|
t.string :title
t.text :description

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddQuizRefToMultipleChoiceQuestions < ActiveRecord::Migration[5.2]
def change
add_reference :multiple_choice_questions, :quiz, foreign_key: true
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_09_21_202046) do
ActiveRecord::Schema.define(version: 2018_10_22_152131) do

create_table "multiple_choice_questions", force: :cascade do |t|
t.string "question"
Expand All @@ -21,6 +21,15 @@
t.string "distractor_4"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "quiz_id"
t.index ["quiz_id"], name: "index_multiple_choice_questions_on_quiz_id"
end

create_table "quizzes", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
5 changes: 5 additions & 0 deletions test/fixtures/multiple_choice_questions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
# question :string
# created_at :datetime not null
# updated_at :datetime not null
# quiz_id :integer
#
# Indexes
#
# index_multiple_choice_questions_on_quiz_id (quiz_id)
#

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/quizzes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# == Schema Information
#
# Table name: quizzes
#
# id :integer not null, primary key
# description :text
# title :string
# created_at :datetime not null
# updated_at :datetime not null
#

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyString
description: MyText

two:
title: MyString
description: MyText
5 changes: 5 additions & 0 deletions test/models/multiple_choice_question_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
# question :string
# created_at :datetime not null
# updated_at :datetime not null
# quiz_id :integer
#
# Indexes
#
# index_multiple_choice_questions_on_quiz_id (quiz_id)
#

require 'test_helper'
Expand Down
18 changes: 18 additions & 0 deletions test/models/quiz_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# == Schema Information
#
# Table name: quizzes
#
# id :integer not null, primary key
# description :text
# title :string
# created_at :datetime not null
# updated_at :datetime not null
#

require 'test_helper'

class QuizTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 5e0f016

Please sign in to comment.