Skip to content

Instantly share code, notes, and snippets.

@jelaniwoods
Last active May 13, 2022 18:20
Show Gist options
  • Save jelaniwoods/a28f154cfe9e473f3837a8d814f6bc16 to your computer and use it in GitHub Desktop.
Save jelaniwoods/a28f154cfe9e473f3837a8d814f6bc16 to your computer and use it in GitHub Desktop.

Very Best Debug

  • README
  • debug home page
    • Reading - Better Errors
      • banner
      • highlighted line
      • console
    • debug it
    • rails sample_data
  • rails grade
    • read test title
    • click on test
    • examine test
  • Handy to keep up RCAV flowchart image
  • Spend at least 30 minutes on this and pg-gui if you haven’t finished
  • then we’ll look at something pretty cool and exciting
    • (2-2:15)

Refactoring MSM 2

  • bin/server visit actor details
  • rails sample_data
  • go over actor details -> filmography view
  • we defined methods that looked up associated records
    • open actor#characters
    • open character#movie
    • open movie#director
  • benefits
    • if we define them first, speeds up view template
    • other teammates can just use them
    • avoid typos re-writing queries everywhere
  • We can use these methods anywhere that we have a record
  • any questions?
  • Each of these associations represent one side of a 1 to N
    • director#filmography vs movie#director
  • Look at movie#director vs character#movie
    • very similar
    • The only things that differ are the
      • name of the method
      • the name of the foreign key column
      • the name of the class
    • Define Character#actor by copying movie # director
  • Similar relationship btw character and actor
    • Define Character#actor by copying movie # director

  • We can define these types of methods
    • in a better, more powerful, and concise way
  • If only three things change between these methods
    • Rails will make our lives easier by
    • letting us use a method that will define other methods for us
  • belongs_to(:movie, {…})
    • comment out both
    • visit page
    • uncomment new one
  • Can you see how Rails can infer and deduce how to define this the director method using the information we gave to belongs_to?
  • A non-technical teammate can read and understand the relationship between the two tables
    • Movie belongs to one director
    • Character belongs to one movie
  • What’s the benefit?
    • even less chance for typos
    • power
      • joins
        • I can query Characters using columns in the character table
        • what about using columns in the movies table?
        • Character.joins(:movie).where("movies.title LIKE '%The Dark Knight%')
  • What do you think?
  • Exclude keys in belongs_to
    • what do you think will happen if I remove the class name?
      • put on debugging hat
    • convention over configuration philosophy
  • This is one type of association method, where our starting record contains a foreign key that connects to another table
    • the other side is different
    • Director#filmography
    • Define Movie#characters by copying Director # filmography
  • has_many(:characters) method constructor

We have these relationships

Director -> Movies
Actor -> Characters
Movie -> Characters

# Define:
characters method in Actor
actor method in Characters

characters method in Movie
movie method in Character

director method in Movie
filmography method in Director
  • Break
  • any questions?
  • go over solutions

association accessors demo

  • find link in readme
  • watch me and ask questions
  • add models (after we come up with our data model)
  • start wizard
    • to help us determine how to define these association methods
    • direct vs indirect
  • tool to walk you through how to define association the long way
    • using the shortcuts
    • and if we can make them more concise
  • interactive chapter for any of your projects

Many to Many

  • go over actor#filmography
  • went over something similar before with movie cast, not used in this app
  • different type of association— -> slides
  • there isn’t a foreign key column used there’s no class name
  • many to many, these two tables are indirectly related through two existing associations
  • we can use has_many for this as well
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment