{"action":"opened","issue":{"url":"https://api.github.com/repos/ga-dc/pbj-project2-mini-lessons-hw/issues/34","labels_url":"https://api.github.com/repos/ga-dc/pbj-project2-mini-lessons-hw/issues/34/labels{/name}","comments_url":"https://api.github.com/repos/ga-dc/pbj-project2-mini-lessons-hw/issues/34/comments","events_url":"https://api.github.com/repos/ga-dc/pbj-project2-mini-lessons-hw/issues/34/events","html_url":"https://github.com/ga-dc/pbj-project2-mini-lessons-hw/issues/34","id":99967262,"number":34,"title":"Erica Andersen's 'Mini Lessons' Homework","user":{"login":"andersenek","id":11844601,"avatar_url":"https://avatars.githubusercontent.com/u/11844601?v=3","gravatar_id":"","url":"https://api.github.com/users/andersenek","html_url":"https://github.com/andersenek","followers_url":"https://api.github.com/users/andersenek/followers","following_url":"https://api.github.com/users/andersenek/following{/other_user}","gists_url":"https://api.github.com/users/andersenek/gists{/gist_id}","starred_url":"https://api.github.com/users/andersenek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andersenek/subscriptions","organizations_url":"https://api.github.com/users/andersenek/orgs","repos_url":"https://api.github.com/users/andersenek/repos","events_url":"https://api.github.com/users/andersenek/events{/privacy}","received_events_url":"https://api.github.com/users/andersenek/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-08-10T04:15:45Z","updated_at":"2015-08-10T04:15:45Z","closed_at":null,"body":"#### Has many Through\r\n\r\n##### 1) What is the role of a join table in a many-to-many relationship?\r\nThe join table allows a many-to-many relationship occur because it will hold the foreign keys of the two models we want to join/reference.\r\n##### 2) What two columns must be present in a join table? \r\nThe foreign key of Model 1 and the foreign key of Model 2 that we want to reference in this join table.\r\n##### 3) Given the example below, edit the code to define a has many :through relationship.\r\n\r\n```ruby \r\nclass Customer < ActiveRecord::Base\r\n  has_many :purchase_products\r\n  has_many :purchases, through: :purchase_products, source: :products\r\nend\r\n\r\nclass Product < ActiveRecord::Base\r\n  has_many :purchase_products\r\n  has_many :purchased_by, through: :products, source: :customer\r\nend\r\n\r\nclass Purchase < ActiveRecord::Base\r\n  belongs_to :customer\r\n  belongs_to :product\r\nend\r\n``` \r\n##### 4) Based on #3, give an example of associating two instances via the join model.\r\n\r\n```ruby\r\n  def add_product\r\n    @customer = Customer.find(params[:id]) # Find the customer id\r\n    @customer.purchase.create(product_id: params[:product_id]) # Of that instance, find purchases and add that product id to the customer's purchase\r\n  end\r\n```\r\n\r\n#### Devise\r\n\r\n##### 1) What does the current_user method that the Devise gem provides?\r\nThis is probably one of my favorite methods that Devise has! It recognizes the current signed in user and returns the model class relating to that user. \r\n##### 2) What does the authenticate_user! method that the Devise gem provides?\r\nThis method ensures a user is logged in. If a user is not, it also ensures that accounts can be created. This method is invoked via a before_filter.\r\n##### 3) Write a signout link using the link_to rails helper and a devise path.\r\n```ruby\r\n<%= link_to \"Sign Out\", destroy_user_session_path, method: :delete %>\r\n```\r\n##### 4) How do I generate a devise model in the terminal?\r\nAfter adding the Gem 'devise', running `bundle install` and `rails generate devise:install`, you can generate a devise model by running `rails generate devise modelname`\r\n##### 5) What are the trade offs for using a gem for authentication over a handrolled solution? (no real right answer)\r\nA gem for authentication can save a lot of time, but also generates a lot of files that you may not necessarily understand or know how to get to. Whereas, handrolling a solution can give you more 'control' about what's being created, although it can often take a much longer time to set up.\r\n\r\n#### Validations\r\n\r\n##### 1) Which component, of Rails MVC, is responsible for the business logic?\r\nThe model is responsible for the business logic in Rails MVC.\r\n##### 2) Assume the user's age is an optional field. Write the validation to verify that a User's age is between 13 and 125 (inclusive).\r\n```ruby\r\nclass User < ActiveRecord::Base\r\n  validates_inclusion_of :age, :in => 13..125,\r\n  :message => \"{{value}} is not a valid age\"\r\n  validates :age, numericality: true\r\nend\r\n```\r\n##### 3) What would user.errors.messages return (for the above User), if you assigned user.age = 12?\r\n`12 is not a valid age`\r\n##### 4) Assume you visit \"/customers/new\" and enter some invalid information. Given this controller code, what url would your browser be on after pressing \"Create Customer\"?\r\n```ruby\r\nclass CustomersController < ApplicationController\r\n  def new\r\n    @customer = Customer.new\r\n  end\r\n\r\n  def create\r\n    @customer = Customer.new(customer_params)\r\n    if @customer.save\r\n      redirect_to customer_path(@customer)\r\n    else\r\n      render :new\r\n    end\r\n  end\r\n  ...\r\n  private\r\n  def customer_params\r\n    params.require(:customer).permit(:first_name, :last_name, :age)\r\n  end\r\nend\r\n```\r\nThe invalid information would lead to the `else` statement in the create method. This would appear to the user as the page being 'refreshed' since it would render \"/customers/new\" again.\r\n\r\n##### 5) Give one reason why we might have the similar validations in the browser, model, and database layer of our application.\r\nDuplication of validations (validations in the browser, model and database layers) is a way to improve site performance. By validating in separate layers you can display errors to the user as well as to the developer. And in regard to security you can prevent data from being improperly added to your database."}}