Skip to content

Instantly share code, notes, and snippets.

@rickpeyton
Last active August 29, 2015 14:15
Show Gist options
  • Save rickpeyton/b2c8115f5536d8fa1a41 to your computer and use it in GitHub Desktop.
Save rickpeyton/b2c8115f5536d8fa1a41 to your computer and use it in GitHub Desktop.
Course 2: Quiz: Lesson 3
1. What's the difference between rendering and redirecting? What's the impact with regards to instance variables, view templates?
__Redirecting sends you off to a new action and clears all of your old instance variables. Rendering loads a view template. and retains your instance variables.__
2. If I need to display a message on the view template, and I'm redirecting, what's the easiest way to accomplish this?
__Use the flash[:notice] helper and store a message in it.__
3. If I need to display a message on the view template, and I'm rendering, what's the easiest way to accomplish this?
__Store the message in an instance variable and display it__
4. Explain how we should save passwords to the database.
__You should tokenize them with the has\_secure\_password method which stores them salted and hashed in the database. This means that not even I as the database administrator have access to the user's password. If they want to reset their password they will have to actually reset it, I can not look it up for them.__
5. What should we do if we have a method that is used in both controllers and views?
__You could put it in the helpers directory either within the application_helpers file or in its own file in that directory.__
6. What is memoization? How is it a performance optimization?
__Memoization uses the ||= syntax and allows you to specify an instance variable on the left side and a database lookup on the right side. That way you can execute the database lookup on the first call and store the result in the instance variable. All subsequent lookups would just use the instance variable and would save you a trip to the database.__
7. If we want to prevent unauthenticated users from creating a new comment on a post, what should we do?
__Require login for access to the new and create actions on teh comment and post controllers. You can set a current user on the model layer and then do a before filter lookup in the controller to restrict access to those areas__
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.
__You would want to create two columns on the table, likeable\_type and likeable\_id. That would give you types of Photo, Video and Post which would be your types and then the corresponding ID of each.__
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).
__You setup a polymorphic association on the Vote model by setting up belongs_to :user_id and belongs_to :voteable, polymorphic: true to form the relationships. Then on the 1 side (such as Post) you would have has_many :votes, as: :voteable. You would put that same line in any model that you wanted to be available to the polymorphic relationship. You will also need has_many :votes on the User table.__
10. What is an ERD diagram, and why do we need it?
__ERD stands for Entity Relationship Diagram and we need it so that we can plan out our database model. It will save us time in the long wrong to make good decisions prior to deploying code. It is more difficult to move database once it is live.__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment