Code Monkey home page Code Monkey logo

flatiron-bnb-associations'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'

If you're not sure how to use these options, make sure to check out the resource links below.

Resources

flatiron-bnb-associations's People

Contributors

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

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flatiron-bnb-associations's Issues

Dependency error

// โ™ฅ bundle exec rspec
WARN: Unresolved specs during Gem::Specification.reset:
activesupport (>= 4.2)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
/usr/local/rvm/gems/ruby-2.3.1/gems/bundler-1.13.6/lib/bundler/runtime.rb:40:in block in setup': You have already activated spring 2.0.0 , but your Gemfile requires spring 1.3.3. Prepending bundle exec` to your command may solve this. (Gem::LoadError)

gemfile issue

after running the commands:
bundle install
learn

There was an issue with the gemfile specification. Short story is I fixed it by having the gemfile.lock require spring 2.0.2 instead of 1.3.3. However, I don't think this error was meant to be a part of the learning process.

I received the following error:

`WARN: Unresolved specs during Gem::Specification.reset:
      activesupport (>= 4.2)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
/home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/bundler-1.15.4/lib/bundler/runtime.rb:317:in `check_for_activated_spec!': You have already activated spring 2.0.2, but your Gemfile requires spring 1.3.3. Prepending `bundle exec` to your command may solve this. (Gem::LoadError)
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/bundler-1.15.4/lib/bundler/runtime.rb:32:in `block in setup'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/bundler-1.15.4/lib/bundler/runtime.rb:27:in `map'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/bundler-1.15.4/lib/bundler/runtime.rb:27:in `setup'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/bundler-1.15.4/lib/bundler.rb:101:in `setup'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/bundler-1.15.4/lib/bundler/setup.rb:9:in `<top (required)>'
	from /home/williamhancharek/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:121:in `require'
	from /home/williamhancharek/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:121:in `require'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/commands.rb:33:in `<module:Spring>'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/commands.rb:4:in `<top (required)>'
	from /home/williamhancharek/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:69:in `require'
	from /home/williamhancharek/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:69:in `require'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/server.rb:9:in `<top (required)>'
	from /home/williamhancharek/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:128:in `require'
	from /home/williamhancharek/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:128:in `rescue in require'
	from /home/williamhancharek/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:39:in `require'
/home/williamhancharek/Documents/learn/flatiron-bnb-associations-cb-000/spec/models/city_spec.rb:3:in `<top (required)>': uninitialized constant City (NameError)
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.2.1/lib/rspec/core/configuration.rb:1226:in `load'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.2.1/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.2.1/lib/rspec/core/configuration.rb:1224:in `each'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.2.1/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.2.1/lib/rspec/core/runner.rb:97:in `setup'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.2.1/lib/rspec/core/runner.rb:85:in `run'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.2.1/lib/rspec/core/runner.rb:70:in `run'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.2.1/lib/rspec/core/runner.rb:38:in `invoke'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.2.1/exe/rspec:4:in `<top (required)>'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `load'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `<main>'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
	from /home/williamhancharek/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'

`

Seeding is weird

Seed task prevents tests for properties to run. Maybe set up seed task to run for specific association tests, so they only blow up when they need to. Also, to add clarity, seed task should set foreign keys using the instance variable's #id method, not but finding it in the Database. It's hard to reason about the associations otherwise.

"nyc" instead of "new_york_city" in spec/models/city_spec

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.