Code Monkey home page Code Monkey logo

displaying-associations-rails-lab's Introduction

Displaying Associations Rails

In the previous unit, we used validations and forms to create a management dashboard for songs and artists. Now we're ready to start connecting those models and using their relationships to display more interesting data.

  • In the controllers, this lab will require you to write show and index actions for both Song and Artist.

  • You'll need to add a foreign key by writing your own migration.

  • You'll need to set up the association in the model classes.

  • You'll need to format and link to the songs and artists according to the specs.

Additionally, you'll find that the specs require two special methods, Artist#song_count and Song#artist_name. These methods are meant to protect the views from complexity that belongs in the model.

They are also a great example of a software design principle called the Law of Demeter, which is sometimes called the "one dot" rule in object-oriented languages like Ruby. In other words, ruby user.best_friend is better than ruby user.friends.find_by(best: true) because all of the "friend"-related complexity is hidden away within the user model. This protects user-related code from future changes to friend functionality.

For example, if the above architecture changed such that best friendship was determined by the highest "friendship" value instead of a boolean best flag, the "two-dots" code would need to be changed everywhere, but the first snippet, which obeys the Law of Demeter, hides that complexity in the User#best_friend method, whose definition can be changed without having to track down and update every single usage.

displaying-associations-rails-lab's People

Watchers

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

displaying-associations-rails-lab's Issues

Schema file includes a column for songs table that does not exist

Current Schema file included in this lab has the foreign key column that needs to be created by the student.
Line 25: t.integer "artist_id"

The database does not actually have this column however.
If a student checks this file and sees this line, they would think to skip the creation of the foreign key column and run into issues later on.

What happened to convention over configuration?

image

This seems to be asking us to set up a custom method that save no keystrokes and no perf over @song.artist.name

Given Rails' fanatical devotion to convention over configuration, why would we do this?

issue

i could not render the view #show/1, any idea why?

with following error:
Showing /home/ICanDoAllThingThroughChrist/code/labs/displaying-associations-rails-lab-v-000/app/views/songs/show.html.erb where line #2 raised:

undefined method `name' for nil:NilClass

Issues running specs

The feature specs would always fail as such...

 Failure/Error: Artist.destroy_all

 NameError:
   uninitialized constant Artist
 # ./spec/features/artists_spec.rb:3:in `block (2 levels) in <top (required)>'

I fixed the issue by adding "require 'rails_helper'" to the feature tests. Not sure if it's required or if it's an issue with my environment.

Minor issue in the songs/edit.html.erb page

Hi. This doesn't affect the lab tests or anything, but there's a syntax problem at the top of the views/songs/edit.html.erb page.

Line 1 says <h1>Editing <%= @song.name %></h1>, but since songs have titles instead of names, that line of code should be <h1>Editing <%= @song.title %></h1>.

This is just an edge case, since the lab doesn't focus on the "new" or "edit" pages at all; I just wanted to let you guys know that it's there.

---Sdcrouse

error: +32 spec/features/artists_spec.rb

There is an error in the specs in line 32 of spec/features/artists_spec.rb.
Instead of expecting 2 songs, the singular 2 song is expected.

 it "shows the song count for each artist" do
   visit artists_path
   expect(page).to have_content("2 song")
 end

unclear directions

This block suggests that we copy the values to test them in our console. The separation between the attributes and the return value is unclear and obscured, and created confusion.

2.2.3 :018 > new_person = Person.new
=> #<Person id: nil, name: nil, created_at: nil, updated_at: nil>

2.2.3 :019 > new_person.addresses_attributes={"0"=>{"street_address_1"=>"33 West 26", "street_address_2"=>"Floor 2", "city"=>"NYC", "state"=>"NY", "zipcode"=>"10004", "address_type"=>"work1"}, "1"=>{"street_address_1"=>"11 Broadway", "street_address_2"=>"Suite 260", "city"=>"NYC", "state"=>"NY", "zipcode"=>"10004", "address_type"=>"work2"}}
=> {"0"=>{"street_address_1"=>"33 West 26", "street_address_2"=>"Floor 2", "city"=>"NYC", "state"=>"NY", "zipcode"=>"10004", "address_type"=>"work1"}, "1"=>{"street_address_1"=>"11 Broadway", "street_address_2"=>"Suite 260", "city"=>"NYC", "state"=>"NY", "zipcode"=>"10004", "address_type"=>"work2"}}

2.2.3 :020 > new_person.save
(0.2ms) begin transaction
SQL (0.8ms) INSERT INTO "people" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2016-01-14 11:57:00.393038"], ["updated_at", "2016-01-14 11:57:00.393038"]]
SQL (0.3ms) INSERT INTO "addresses" ("street_address_1", "street_address_2", "city", "state", "zipcode", "address_type", "person_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["street_address_1", "33 West 26"], ["street_address_2", "Floor 2"], ["city", "NYC"], ["state", "NY"], ["zipcode", "10004"], ["address_type", "work1"], ["person_id", 3], ["created_at", "2016-01-14 11:57:00.403152"], ["updated_at", "2016-01-14 11:57:00.403152"]]
SQL (0.1ms) INSERT INTO "addresses" ("street_address_1", "street_address_2", "city", "state", "zipcode", "address_type", "person_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["street_address_1", "11 Broadway"], ["street_address_2", "Suite 260"], ["city", "NYC"], ["state", "NY"], ["zipcode", "10004"], ["address_type", "work2"], ["person_id", 3], ["created_at", "2016-01-14 11:57:00.405973"], ["updated_at", "2016-01-14 11:57:00.405973"]]
(0.6ms) commit transaction
=> true

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.