Skip to content

Instantly share code, notes, and snippets.

@dcousette
Last active March 11, 2016 03:24
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 dcousette/fb0e6d242708ca41c52d to your computer and use it in GitHub Desktop.
Save dcousette/fb0e6d242708ca41c52d to your computer and use it in GitHub Desktop.
Tealeaf Course 2 Quiz: Lesson 3
Tealeaf Course 2 - Lesson 3 Quiz
Quiz: Lesson 3
1. The difference between rendering and redirecting is in what the request actually returns. With a render you are receiving a payload which consists of markup to display on a page along with other code (css, javascript, etc). With a redirect the request is returning a URL, which the browser then knows how to follow (which issues a new request). The impact of these two http return types on instance variables and view templates are that instance variables can only be displayed in a view when rendering a template. Redirects only contain url data for a new request.
2. The easiest way to display a message on the view template when redirecting is by storing a value in the flash and then displaying it in the layout template between requests.
3. The easiset was to display a message on a view template when rendering is to include the message in the template itself using erb, html, or string interpolation.
4. We should save passwords by first hashing them using an algorithm like bcrypt. This is far more secure than saving raw passwords into a database, which we should never do. Hashing the password converts it into a random string. To authenticate a user provides their password which is hashed and compared with the data stored on the database. This way no one can "retrieve" a lost password.
What should we do if we have a method that is used in both controllers and views?
5. If we have a method that is used in both controllers and views we should define the method in the ApplicationController and make it available to the views using helper_method (ex. helper_method :method_name).
What is memoization? How is it a performance optimization?
6. Memoization is the concept of using the conditional assignment operator ||= to prevent multiple calls to the database for an object.
Like this: @object ||= Table.find(params[:id]) if params[:id]
This is a performance optimization because when an object is used in a template several times the database will only be called once for the object - and then assigned. On subsequent appearances in the template the object's previously assigned value is used instead of making a new call to the database each time.
7. If we want to prevent unauthenticated users from creating a new comment on a post, we should create a method to check for authentication and then create a before action using this method in the controller.
For ex. before_action :logged_in?, only: [:new, :create]
8. To make this table polymorphic we would create a new table for likes that looks like this:
id user_id likeable_type likeable_id
1 4 video 12
2 7 post 3
3 2 photo 6
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).
9. In the above example we would setup the polymorphic association in the model like such:
class Like < ActiveRecord::Base
belongs_to :voteable, polymorphic: true
end
class Video < ActiveRecord::Base
has_many :likes, as: :voteable
end
10. An ERD diagram or Entity Relationship Diagram is a visual mapping of an application's data structure. It is a planning tool, we use it to list out the proposed database tables, columns and associations our app will need. The ERD diagram helps us clearly understand the data structure our app requires.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment