Git Product home page Git Product logo

flatiron-bnb-associations-immersive-alum's Introduction

Bonus Lab: Flatiron-BnB Associations

We're going to build Airbnb. Really. We're going to take this in steps. First let's work on our model associations and write migrations. This will be challenging, but doable. Take it slow and work together. Follow the model specs.

Before anything, note that when you generate models, controllers, etc, be sure to include this option, so that it skips tests (which we already have): --no-test-framework

Where to Begin

First 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'

Resources

Foreign key and Class Name AR class methods

flatiron-bnb-associations-immersive-alum's People

Contributors

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

Watchers

 avatar James Cloos avatar Mat Balez avatar Victoria Thevenot avatar  avatar Joe Cardarelli avatar Taranjyot Singh avatar Sara Tibbetts avatar  avatar  avatar  avatar Antoin avatar Nicole Kroese  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.