Git Product home page Git Product logo

twitter-follows-readme-nyc-web-career-012819's Introduction

Twitter Follows

Objectives

  1. Create a complex Domain Model
  2. Implement an active follow relationship (following )
  3. Implement the passive relationship (i.e. followers )

Instructions

In Twitter, a user follows many other users. That person can be followed by many people. For example, I follow @coffeedad and @cher. @coffeedad has many followers and so does @cher.

How you would you domain model this out?

A user can either follow or be followed by another user. Since this relationship can be true of many users, we have a many-to-many relationship between users. Consequently, we need at least two models: the User model and a join model. Creating another model for users (like a followers model) would be redundant, as it would replicate the information present in the User model. A user has many other users through relationships, for a total of two models.

What tables/foreign keys would you need?

We determined previously that we need two models. In Active Record, each model corresponds to a table. We will have a Users table and a Relationships table.

class User < ActiveRecord::Migration[5.2]
  def change
    create_table :users do |t|
      t.string :username
      t.timestamps
    end
  end
end

For the sake of this exercise, we will keep the attributes of the User table simple, with just a username.

class CreateRelationships < ActiveRecord::Migration[5.2]
  def change
    create_table :relationships do |t|
      t.integer :follower_id
      t.integer :followed_id
      t.timestamps
    end
  end
end

The Relationships table introduces more interesting elements, with us specifying a columns for the follower and followed user ids.

How would you domain model this out?

class Relationship < ActiveRecord::Base
  belongs_to :followed, class_name: "User", foreign_key: :followed_id
  belongs_to :follower, class_name: "User", foreign_key: :follower_id
end

In defining our ActiveRecord class, we begin implementing a self join. We must specify the name of the class/model to which the relationship belongs, as well as the foreign key.

class User < ActiveRecord::Base
  has_many :active_relationships, class_name: "Relationship", foreign_key: :follower_id, dependent: :destroy
  has_many :followeds, through: :active_relationships, source: :followed
  has_many :passive_relationships, class_name: "Relationship", foreign_key: :followed_id, dependent: :destroy
  has_many :followers, through: :passive_relationships, source: :follower
end

We close out the definition of this relationship by implementing our many-to-many self join of users. Like with the Relationship model, we create an alias/method for the relationship between users.

Resources

View this lesson on Learn.co

twitter-follows-readme-nyc-web-career-012819's People

Contributors

ipc103 avatar levimllr avatar campbelllsssoup avatar

Watchers

 avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar  avatar Matt avatar Antoin avatar  avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.