Created
May 23, 2014 12:58
-
-
Save kafaichoi/638f14c1db44ab787a0d to your computer and use it in GitHub Desktop.
answer_for_quiz3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Quiz: Lesson 3 | |
1. What's the difference between rendering and redirecting? What's the impact with regards to instance variables, view templates? | |
They both complete a request but redirect will tell the client (browser) to send a brand-new request to a new URL. Therefore, no instance variable from previous action triggering the redirect will be available to the redirected view template . For rendering, the instance variable from the action will be avaiable for the view template being rendered. | |
2. If I need to display a message on the view template, and I'm redirecting, what's the easiest way to accomplish this? | |
flash[:notice] | |
3.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] | |
4.Explain how we should save passwords to the database. | |
The password should never be saved in database directly which means no ones can take the password out. We can using a one-way hashing algorithmn. When the user enter their passwords, they convert to a sequence of unidentified number which will be stored in the column called password_digest. When a user try to login, the password entered will be converted again to password_digest and we can compare them to see if the password is correct. | |
5.What should we do if we have a method that is used in both controllers and views? | |
We put the method in the Parent class ApplicationController and pass the those methods to helper_method. | |
6.What is memoization? How is it a performance optimization? | |
memoization is a optimization technique where the result is from usually a pure function/method will be save. It can prevent function with same paramemter runs again and also prevent from hitting databse. One easy technique in rails is using operator ||=. | |
7. If we want to prevent unauthenticated users from creating a new comment on a post, what should we do? | |
We need set up some helper method like require_same_user in controller. We specify these method in before_action only for specific action we want. | |
8. 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 use commentable_type and commentable_id as a composite foreign key to replace photo_id, video_id and post_id. | |
9. 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 | |
10. What is an ERD diagram, and why do we need it? | |
ERD stands for Entity Relational Diagram. It is the visual representation of the associations(in a schema layer) between different tables in a database. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment