Skip to content

Instantly share code, notes, and snippets.

@jelaniwoods
Last active May 6, 2022 18:03
Show Gist options
  • Save jelaniwoods/5f5ebac810770c6f8c92de3d5ec76ece to your computer and use it in GitHub Desktop.
Save jelaniwoods/5f5ebac810770c6f8c92de3d5ec76ece to your computer and use it in GitHub Desktop.

AD1 Day 6

Refactoring MSM Queries

  • create the assignment
    • the starting point here is the ending point for msm-queries
    • take a few minutes to explore the solution
      • ask questions if anything is unclear or you struggled with a particular part of the assignment
      • maybe you did something different than what I did in the solutions, and I love to hear your approach to.
  • Adding data
    • now that our projects have databases, you may have noticed that is really hard to test our app when we don’t have any data
    • we could add data:
      • through /rails/db demo
      • through rails console demo
      • but both of those are kind of time consuming
    • Raghu, Pat, and I made a small ruby program you can run to reset your database with sample data
      • This is good practice for any development team to do, write a file that adds sample data to the development db
      • rails sample_data
      • visit actors#show
  • Actor details
    • go over RCAV steps
    • show action
      • fetch from params
      • Actor.where, (Look in actors table,
      • { :id => the_id }, (Look in the id column
      • for a value of the_id
      • this filters down the list of each row/record in the Actors table
      • where returns a list ALWAYS
      • we (as devs) know that id values are unique, and there is at most one result (there could be zero)
        • so we can use .at(0) to retrieve the first record from the filtered list
      • render the template
    • View
      • using ERB tags to display the different attributes of the actor where it makes sense
        • for the img, we can inject an attribute of the record into an HTML attribute
          • attributes aren’t always the content of HTML elements, sometimes they are HTML attributes
    • During this arc of the course, the most common issue students face is thinking that .where will return one record— it doesn’t
      • that’s why I try to keep one method call per line and name my variables descriptively, so I don’t get confused on what values are stored in the variables and what the method returns.
    • Actor Filmography
      • probably the hardest part of this project
      • if you struggled thinking through this part, remember that navigating through a db is the same as what we did in week 1 and 2, when we had paper tables
      • always come up with a plan of attack before you write code,
        • it’s a lot harder to try and just visualize in your mind, what your code needs to do
        • use /rails/db, use comments in models
      • Character, is the join table where each row connects one movie to one actor
        • if we start from an actor, we can filter the Character table for just the records that have the id of the actor, stored in their actor_id column.
        • Then from that list of characters, we can loop over them,
          • take the value from its movie_id column
          • go to movies table, look in the id column for a match
          • take the first match and display the movie
        • demo in /rails/db
    • Refactoring
      • important term in software development
        • you don’t add or remove features
        • you re-write existing code to make it more maintainable
          • sometimes that’s performance
          • making code more readable
            • like in refactoring fortune teller
      • devs are hesitant to refactor code, b/c they don’t want to accidentally break something or introduce bugs.
        • how do we prevent ourselves from breaking our existing functionality?
        • solution: automated tests
      • rails grade is just running a set of tests that we wrote
        • running a command bundle exec rspec
      • each of these tests scrape your own application and check for particular content or html elements
      • so in the future if you or your team of devs is interested in building long term support for your app, there should be a set of tests created for each feature that exists in your app.
        • so new features don’t end up breaking any existing functionality
        • I want to emphasize this b/c
          • I never learned about this when I first started coding
          • I ran into this exact issue where new features kept breaking old features
        • tests often take longer to write then the actual new feature,
          • not always important if it’s a throwaway proof of concept
          • but a big red flag if you are working on / managing a team of devs for a long term project and they AREN’T writing tests
      • So onto this assignment!
        • How will we refactor this code?
        • Reminder about instance methods
          • did them in ruby chapters
          • I showed you in the API demo the Geocoder.street_to_coords instance method
            • this made it easy for anyone else to use
            • they didn’t need to know how it works
        • this is how we’ll refactor this app— with instance methods
      • actors#show
        • let’s say I actually want to display the movie title with the year it was released.
          • i want to do that basically everywhere in the app
          • it’d be a real pain to copy paste this code everywhere… it’d be nice if I had a method that did this work, called title_with_year
          • If I try to call this now I get an error
        • so I can go define it
          • hardcode to return example movie,year
          • error goes away, but now we need to actually made it work— how?
          • can I just do:
          • m.title + "(" + m.year.to_s + ")"
          • in rails c, if I run this method with
          • m = Movie.all.sample
          • m.title_with_year
          • do you think this will work?
            • if not, what do you think the error message will say?
            • so what is m in the method?
            • I could name my variable that stores a movie anything, not just m.
          • does anyone remember how to resolve this?
            • self, is a placeholder for the object that the method was called on
            • any questions?
      • Now to actually refactor
        • I want to improve these queries that we wrote.
          • from a movie, we want to display the name of the director
      • Movie#my_director
        • now i can replace this in all the view templates that I have a movie and want to display the director
        • imagine if we had these methods at the beginning and didn’t even need to struggle writing queries in the controller and view templates
          • speeds us up so much
          • it’s the first thing I do, when I’m starting a new project
            • make tables, models
            • add association methods
            • then I can build the views
      • Director#filmography
        • other side of the one to many between movie and director
      • Stretch goal -> Movie#cast
        • side of a many to many

AD1 Day 6

  • Open Assignment
  • Actor details page
  • What is Refactoring
    • automated tests
  • How will we refactor this app?
    • Movie#title_with_year
    • self?
  • Movie#my_director
  • Director#filmography (other side of 1-N)
  • Lab to catch up with me, and do others
  • Stretch goal
    • Movie#cast -> a many-to-many
    • define characters first
    • use exisitng methods, to build up bigger and more complicated methods
    • if the long way made sense to you— keep doing it that way
    • this is a powerful technique we'll see more of moving forwards
  • Homework:
    • Adding GUI's to our db, forms
    • Next week, we'll do sign in/sign out

1:30 - 4:30 3pm break


add js to poll to see if fork created successfully

  • just makes get to repo url, if status is 404 then fork is not ready
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment