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

Commit

Permalink
Creating an Ownership Association between User and Quiz
Browse files Browse the repository at this point in the history
  • Loading branch information
sdflem committed Feb 28, 2020
1 parent db0c329 commit 8f9bc5e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/models/quiz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
# title :string
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint
#
# Indexes
#
# index_quizzes_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (user_id => users.id)
#

class Quiz < ApplicationRecord
Expand All @@ -19,6 +28,13 @@ class Quiz < ApplicationRecord
dependent: :destroy # if a quiz is destroyed, also destroy all of its questions
)

belongs_to(
:creator,
class_name: 'User',
foreign_key: 'user_id',
inverse_of: :quizzes
)

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

Expand Down
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable

has_many(
:quizzes,
class_name: 'Quiz',
foreign_key: 'user_id',
inverse_of: :creator
)

end
5 changes: 5 additions & 0 deletions db/migrate/20200228211653_add_user_fk_col_to_quizzes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserFkColToQuizzes < ActiveRecord::Migration[6.0]
def change
add_reference :quizzes, :user, foreign_key: true
end
end
5 changes: 4 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: 2020_02_28_205629) do
ActiveRecord::Schema.define(version: 2020_02_28_211653) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -31,6 +31,8 @@
t.text "description"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "user_id"
t.index ["user_id"], name: "index_quizzes_on_user_id"
end

create_table "users", force: :cascade do |t|
Expand All @@ -46,4 +48,5 @@
end

add_foreign_key "mc_questions", "quizzes"
add_foreign_key "quizzes", "users"
end
12 changes: 12 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)

u1 = User.create!(
email: 'alice@gmail.com',
password: 'password'
)

u2 = User.create!(
email: 'bob@gmail.com',
password: 'password'
)

quiz1 = Quiz.create!(
creator: u1,
title: 'MVC Concepts',
description: 'This quiz covers concepts related to the Model-View-Controller web application architecture.'
)

quiz2 = Quiz.create!(
creator: u2,
title: 'Rails Concepts',
description: 'This quiz covers concepts related to web application development using the Ruby on Rails platform.'
)
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/quizzes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
# title :string
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint
#
# Indexes
#
# index_quizzes_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (user_id => users.id)
#

# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
Expand Down
9 changes: 9 additions & 0 deletions test/models/quiz_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
# title :string
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint
#
# Indexes
#
# index_quizzes_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (user_id => users.id)
#

require 'test_helper'
Expand Down

0 comments on commit 8f9bc5e

Please sign in to comment.