DEV Community

Cover image for Database setup and associations for simple quiz app
Milan Zivkovic
Milan Zivkovic

Posted on

7 2

Database setup and associations for simple quiz app

Intro

Building a simple quiz application started with a blank diagram and cognition that my SQL knowledge is fading (if I ever had any). I'll just say that I had to refresh on lectures on join tables (inner join tables to be precise).

What you will find in this blog

Here you will find the database diagram and model associations. I hope it will give you guidance for your quiz application. Worth mentioning is that backend side was created in Rails with --api flag (generators will not create views folder, frontend side will be written separately )
Database scheme

Models

  1. Quiz
  2. Question
  3. Option
  4. User
  5. Take
  6. Response

Model Associations

Quiz

has_many :questions
has_many :takes
has_many :users, through: :takes
Enter fullscreen mode Exit fullscreen mode

Question

belongs_to :quiz
has_many :options
has_many :responses
Enter fullscreen mode Exit fullscreen mode

Option

belongs_to :question
has_many :responses
Enter fullscreen mode Exit fullscreen mode

User (with basic validations)

has_many :takes
has_many :quizzes, through: :takes
validates :username, presence: true
validates :email, presence: true
validates :email, uniqueness: true
Enter fullscreen mode Exit fullscreen mode

Take

belongs_to :user
belongs_to :quiz
has_many :responses, dependent: :destroy

Enter fullscreen mode Exit fullscreen mode

Response

belongs_to :take
belongs_to :option
belongs_to :question
Enter fullscreen mode Exit fullscreen mode

Summary

Having Takes table as a join table between Users and Quizzes will give as an option that user can retake the quiz as many times as he/she wants.
Each response will belong to take, question and option. Here we can distinguish responses for the same question with the same option selected but that happens it different takes.

API Trace View

Struggling with slow API calls?

Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay