Skip to content

Instantly share code, notes, and snippets.

@jamesyang124
Last active December 27, 2015 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesyang124/7235941 to your computer and use it in GitHub Desktop.
Save jamesyang124/7235941 to your computer and use it in GitHub Desktop.

Rails Week 3 Quiz

  1. What's the difference between rendering and redirecting? What's the impact with regards to instance variables, view templates?
  • Render only refresh by templates, redirect_to will go the action controller methods. So the instance variables and params will reset by redirect_to, and render will not reset instance variable and params.
  1. If I need to display a message on the view template, and I'm redirecting, what's the easiest way to accomplish this?
  redirect_to @post, notice: 'message' 
  1. If I need to display a message on the view template, and I'm rendering, what's the easiest way to accomplish this?
  flash.now[:notice] = 'message'
  render 'layouts/template'
  1. Explain how we should save passwords to the database.
  • Create an attribute named with 'password_digest' in database table.
  • add has_secure_password validations: false to the model, the validations set false to provide flexibility.
  • 'gem install bcrypt-ruby'
  • For the model, call virtual attribute model.password and setter model.password_digest
  • Never store the password in app, so validate the password by model.authenticate('password_input').
  1. What should we do if we have a method that is used in both controllers and views?
  • Put it to application controller, and declare it by helper_method: this_method. This helper is for both view and controller and should work for redirect_to but not render[render will not go action].
  1. What is memoization? How is it a performance optimization?
  • Optimization for reducing times of queries from database. During each session, we can cache it by instance variable rather than hit the database frequently.
  # In ApplicationController
  def current_user
    @insance ||= Model.find(session[:user_id]) if session[:user_id]
  end
  1. If we want to prevent unauthenticated users from creating a new comment on a post, what should we do?
  • Set-up application level authentication.
  # In ApplicationController, helper_methods
  def logged_in?
    current_user
  end

  def require_user 
    if !logged_in?
      flash[:error] = 'error message'
      redirect_to root_path
    end
  end

  # Set before_action for specific action in action controller
  before_action: :require_user, except: :index, :show
  
  # For view, add authentication in html.erb
  <% if logged_in? %>
    <!-- code here -->
  <% end%>
  1. Suppose we have the following table for tracking "likes" in our application. How can we make this table polymorphic? Note that the "user_id" foreign key is tracking who created the like.

    id user_id photo_id video_id post_id
    1 4 12
    2 7 3
    3 2 6
  • We can build the table as:

    id user_id media_type media_id
    1 4 video 12
    2 7 post 3
    3 2 photo 6

Run rake g migration polymorphic_creation

  # In migration file, add:
  t.string :media_type
  t.integer :media_id
  # or t.references :media, polymorphic: true

  # In model, associate both side as one of follows:
  class Like < ActiveRecord::Base 
    belongs_to :user
    belongs_to :media_type, polymorphic: true
  end
  
  class User < ActiveRecord::Base 
    has_many :likes, as: :media_type
  end
  1. How do we set up polymorphic associations at the model layer? Give example for the polymorphic model (eg, Vote) as well as an example parent model (the model on the 1 side, eg, Post).
  class Vote < ActiveRecord::Base
    belongs_to :voteable, polymorphic: true
  end
 
  class Post < ActiveRecord::Base    
    has_many   :votes, as: :voteable
  end
  1. What is an ERD diagram, and why do we need it?
  • Entity relationship diagram, It builds for clarifing and describing the associations between the data models.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment