Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Creating a One-to-Many Association between the Quiz and McQuestion Mo…
Browse files Browse the repository at this point in the history
…del Classes
  • Loading branch information
sdflem committed Feb 23, 2020
1 parent 08f4453 commit 92f89db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/mc_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
#

class McQuestion < ApplicationRecord

belongs_to(
:quiz, # McQuestion attribute of with datatype Quiz
class_name: 'Quiz', # datatype of attribute
foreign_key: 'quiz_id', # name of column containing FK
inverse_of: :mc_questions # attribute on other side of association (array containing all McQuestion objects belonging to a quiz)
)

validates :question,
presence: true,
uniqueness: true
Expand Down
10 changes: 10 additions & 0 deletions app/models/quiz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
#

class Quiz < ApplicationRecord

has_many(
:mc_questions, # Quiz attribute containing an array of McQuestion objects
class_name: 'McQuestion', # datatype of attribute
foreign_key: 'quiz_id', # name of column containing FK in other table
inverse_of: :quiz, # attribute on other side of association (parent Quiz object)
dependent: :destroy # if a quiz is destroyed, also destroy all of its questions
)

validates :title, presence: true
validates :description, presence: true

end

0 comments on commit 92f89db

Please sign in to comment.