0

at the moment I'm doing Odin Project's Ruby on Rails track (didn't know such a resource was available until a few days ago.) and it has a few scenarios and they want you to write the appropriate data modeling plus association for them. Unfortunately from what I've been able to find, they don't have any answer key for the 4 scenarios they give you. I was just wondering if someone can look over my answers and guide me to the right direct on what I did wrong and/or right if that's not too much to ask.

These are the four scenarios from Odin Project

  1. You are building an online learning platform (much like this!). You've got many different courses, each with a title and description, and each course has multiple lessons. Lesson content consists of a title and body text.

  2. You are building the profile tab for a new user on your site. You are already storing your user's username and email, but now you want to collect demographic information like city, state, country, age and gender. Think -- how many profiles should a user have? How would you relate this to the User model?

  3. You want to build a virtual pinboard, so you'll have users on your platform who can create "pins". Each pin will contain the URL to an image on the web. Users can comment on pins (but can't comment on comments).

  4. You want to build a message board like Hacker News. Users can post links. Other users can comment on these submissions or comment on the comments. How would you make sure a comment knows where in the hierarchy it lives?

Here's what I come up with. The fourth one completely stumped me unfortunately.

1. table - course
    t.string :title
    t.text :description
    has_many :lessons

table - lesson
    t.string :title
    t.text :body
    belongs_to :course
    foreign key :course_id

2. AddDemo_to_User city:string state:string country: string age:integer gender:string

3. table - pin
     url:string
     comment:text
     user has_many :pins
     pin belongs_to :user
     foreign_key :user_id
Dipet
  • 323
  • 3
  • 14

1 Answers1

1

The fourth one is fairly open ended. What it's asking of you is hierarchy. Each comment record in the database needs to know who its parent is.

The simple way is to use something called an adjacency list.

posts
  url:string
  user_id:integer

comments
  post_id:integer
  parent_id:integer # this will point to the parent comment of this comment. If it's null, then this is a 'root' comment, meaning it's not a comment of a comment
  message:text

This is just one way to keep track of hierarchy. You can learn about them more here: What are the options for storing hierarchical data in a relational database?

Community
  • 1
  • 1
matthewdu
  • 43
  • 4