Coder Social home page Coder Social logo

marykang09 / active-record-practice-flatiron-bnb-lab-dc-web-030920 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learn-co-students/active-record-practice-flatiron-bnb-lab-dc-web-030920

0.0 1.0 0.0 110 KB

License: Other

Ruby 82.55% JavaScript 1.86% CSS 1.91% HTML 13.68%

active-record-practice-flatiron-bnb-lab-dc-web-030920's Introduction

Flatiron-bnb: Associations

Message to dc-web-030920

Hey all, since we're doing this lab remote, have 1 person in your group (someone with reliable internet) fork and clone this repo down. That person should open up the lab in VS Code and then start a Live Sharing with the rest of the group. Be sure to push up to github frequently so everyone has the latest version in case interet goes down

When everyone is good and set up, in your shared terminal, run learn --f-f to see tests all fail. If that doesn't work, run rspec --f-f. Take a good hard look at the tests and read the instructions below to figure out what your domain is. We'll give you a hint, you'll be making 6 files for starters.

You must create files city.rb listing.rb user.rb neighborhood.rb review.rb reservation.rb in the app/models folder. This is where you'll be writing your ruby classes.

How these models are all related is up to you all to figure out. Maybe have someone in the group start a shared whiteboard through zoom in your breakout rooms. Only after you've drawn out all of your models and relationships can you start coding.

Take it slow and work together. Follow the model specs. It's possible to complete this lab with no methods in models and to only use Active Record macros ( has_many: and belongs_to: )

Where to Begin

Work through this lab slowly. For every class you create, you should create a migration file to make a table for that class. Run rake db:migrate RAILS_ENV=test to migrate your DB to get the tests to work, and if you make mistakes, run rake db:migrate:reset RAILS_ENV=test to start over. You can always open test.sqlite3 in DB Browser to look at your test database.

In addition, think about the relations between all of the objects. Let's work through Users and Listings, and from there you should know some cool ActiveRecord tricks to get started on the rest.

We have a user object but a listing belongs to a specific type of user: a host. And the reservation (aka a trip) and a review both belong to a specific type of user: a guest. But we don't want to make two tables for a host and a guest. One way to do this is to create an association where, for example, a listing belongs to a host:

belongs_to :host

Active Record's belongs_to method is going to look for a table named "host", but that doesn't exist. Instead, we need to tell ActiveRecord which table host is referring to:

belongs_to :host, :class_name => "User"

The listings table is instead going to have a column for the foreign key called host_id.

Then on the User class, when we're creating a relationship with listings, where a user has_many listings, we need to specify which foreign key to look for on the listings table, otherwise, ActiveRecord is going to default to looking for a user_id when we named it host_id:

has_many :listings, :foreign_key => 'host_id'

BEFORE THE END OF THE DAY, PUSH UP THE LATEST CODE TO GITHUB so that everyone in the group has access to the latest code.

Resources

Foreign key and Class Name AR class methods

View Flatiron-bnb: Associations on Learn.co and start learning to code for free.

active-record-practice-flatiron-bnb-lab-dc-web-030920's People

Contributors

ahimmelstoss avatar danielseehausen avatar fislabstest avatar fs-lms-test-bot avatar irmiller22 avatar johann avatar lkwlala avatar maxwellbenton avatar octosteve avatar pletcher avatar sarogers avatar sophiedebenedetto avatar thuyanduong-flatiron avatar tsiege avatar

Watchers

 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.