Skip to content

Instantly share code, notes, and snippets.

@mollybrown
Last active January 11, 2017 00:03
Show Gist options
  • Save mollybrown/e310e693bb735495f88f968a72509228 to your computer and use it in GitHub Desktop.
Save mollybrown/e310e693bb735495f88f968a72509228 to your computer and use it in GitHub Desktop.
Overview of Mod 2 Concepts
## Week One - Module 2 Recap
1. List the five common HTTP verbs and what the purpose is of each verb.
- GET: Ask the server for a resource.
- POST: Submit data to the server.
- PUT: Create or update (in whole) a resource on the server.
- PATCH: Update (in part) a resource on the server.
- DELETE: Destroy a resource on the server.
2. What is Sinatra?
- Sinatra is a DSL which responds to HTTP requests and is used for web development.
4. What is MVC?
- MVC stands for model-view-controller, which is a pattern for creating user interfaces for an application. The model contains the data and logic for the application, the view renders the output for the user, and the controller manages changes to the model and view based on user input.
5. Why do we follow conventions when creating our actions/path names in our Sinatra routes?
- So that other people can know what our application is doing quickly and without exessive explanation needed.
6. What types of variables are accessible in our view templates without explicitly passing them?
- Instance variables.
7. Given the following block of code, how would I pass an instance variable `count` with a value of `1` to my `index.erb` template?
```ruby
get '/horses' do
@count = 1
erb :index
end
```
8. In the same code block, how would I pass a local variable `name` with a value of `Mr. Ed`?
```ruby
get '/horses' do
erb :index, :locals => {:name => 'Mr. Ed'}
end
```
9. What's the purpose of ERB?
- ERB templates allow us to embed Ruby code within an HTML file and make dynamic changes.
10. Why do I need a development AND test database?
- The test database is needed to sandbox dummy data that needs to be entered into a database in order to carry out testing. Development should be more representative of what data will be present in production.
11. What's responsive design?
- "Responsive Design" refers to designing webpages/websites so that the display of content is sensitive to the device you are viewing the content on. In other words, the display will be different whether you access the site on a phone or a desktop.
12. What is CRUD and why is it important?
- CRUD stands for Create, Read, Update, Destroy/Delete and describes the fundamental actions you can perform on a database.
13. What does HTTP stand for?
- HyperText Transfer Protocol.
14. What are the two ways to interpolate Ruby in an ERB view template? What's the difference between these two ways?
- <%= ruby_code %> Prints to the browser and returns a value.
- <% ruby_code %> Is evaluated but does not display in the browser.
15. What's an ORM?
- Object-relational Mapping tool. Used to interact with a database by wrapping raw SQL queries in another programming language.
16. What's the most commonly used ORM?
- ActiveRecord (in Ruby)
17. Let's say we have an application with restaurants. There are seven verb + path combinations necessary to provide full CRUD functionality for our restaurant application. List each of the seven combinations, and explain what each is for.
- See all restaurants | GET | "/restaurants"
- See one restaurant | GET | "restaurants/:id"
- See form to enter new restaurant | GET | "/restaurants/new"
- Submit form to save new restaurant | POST | "/restaurants"
- See form to edit existing restaurant info | GET | "/restaurants/:id/edit"
- Submit form to update a restaurant | PUT/PATCH | "/restaurants/:id"
- Delete a restaurant | DELETE | "/restaurants/:id"
18. What's a migration?
- ActiveRecrod migrations allow you to create and modify database schema
19. When you create a migration, does it automatically modify your database?
- No. You have to run the migration using a rake command (rake db:migrate)
20. How does a model relate to a database?
- The model relates to a table in the database, and the relation of its tables to other tables.
21. What's the difference between agile workflow and waterfall method?
- Agile is an iterative approach in which a product is developed, tested, and released to production in small steps that build upon one another. The waterfall approach involves moving through development, testing, and release of the entire project in a linear fashion where each stage of the project cannot be started until the one before it has been completed.
22. What is the difference between `#new` and `#create`?
- `#new` creates a new instance but does NOT save it to the database (need to call `#save` after)
- `#create` creates a new instance and saves it to the database in one statement
## Week Two - Module 2 Recap
1. At a high level, what is ActiveRecord? What does it do/allow you to do?
- ActiveRecord is Ruby ORM that allows us to interact with/manipulate data in a database as we would a regular Ruby object.
2. What kind of methods are `belongs_to`, and `has_many`? (i.e. class or instance) Give an example.
- `belongs_to` and `has_many` are class variables that create association between other models(classes) in ActiveRecord. EX: A Book(model) belongs_to an Author(model), and a Author has_many Books.
3. What do they allow you to do?
- They allow us to create direct connections between different ActiveRecord models.
4. What's the difference between agile workflow and waterfall method?
- Agile is based upon iterative build/test/release cycles; waterfall is based on completing each major phase of a project before moving on to the next (non-iterative).
5. What is the difference between `#new` and `#create`?
- `#new` creates a new object, but does not save it to the database. `#create` creates a new object AND saves it to the database.
6. At a basic level, what does cURL allow you to do?
- cURL allows us to get and send files over different internet file transfer protocols using the command line.
7. In a database that's holding students and teachers, what will be the relationship between students and teachers? Draw the schema diagram.
- Not sure if this is supposed to be a trick question... A database with only teachers and students is missing the element that actually creates the relationship between teachers and students, i.e. classes. Simply having a teacher model and a student table would be fairly useless in the normal way that we think about the student/teacher relationship.
- ![Schema Diagram](https://www.dropbox.com/s/hvxuc7p21pddjxh/Screen%20Shot%202016-12-11%20at%209.11.11%20PM.png)
8. Define foreign key, primary key, and schema.
- Foregin key: Column used to reference the primary key of another table.
- Primary key: Column used to uniquely identify all table records(rows).
- Schema: The current structure of the database
9. Describe the relationship between a foreign key on one table and a primary key on another table.
- The foreign key on one table refers to the primary key of another table.
10. What are the parts of an HTTP response?
- A status line containing the HTTP version and response code, followed by response headers, follwed by an optional response message body.
11. Describe some techniques to make our Sinatra code more DRY. Give an example of when you would use these techniques.
- So far I have seen this in the form of extracting logic related to routing out into modules, using view partials, and in taking advantage of model associations to prevent the need for excess ruby code when running db queries. However, none of these are specific to Sinatra, so maybe I am missing something?
### Optional Questions
1. Name your five favorite ActiveRecord methods (i.e. methods your models inherit from ActiveRecord) and describe what they do.
2. Name your three favorite ActiveRecord rake tasks and describe what they do.
4. What can you expect from a group as you begin working together? As you continue working together?
5. What two columns does `t.timestamps null: false` create in our database?
6. What cURL flag can you use to send a `POST` request?
7. What case does JSON (and JavaScript) use for multi-word variables?
8. What case does Ruby use for multi-word variables?
9. In a database that's holding schools and teachers, what will be the relationship between schools and teachers?
10. In the same database, what will you need to do to create this relationship (draw a schema diagram)?
11. Give an example of when you might want to store information besides ids on a join table.
12. Describe and diagram the relationship between patients and doctors.
13. Describe and diagram the relationship between museums and original_paintings.
14. What are some examples of acceptable values for the parts of an HTTP response?
15. What types of output do we want to test when we test our controllers?
16. What could you see in your code that would make you think you might want to create a partial?
17. Why might you use a helper method?
## Week Three Recap
1. What is the entry at the command line to create a new rails app?
- `rails new project_name --optional --flags`
2. What do Models generally inherit from in rails?
- The ApplicationRecord inhrets from ActiveRecord::Base, other models generally inheret from ApplicationRecord.
3. What do Controllers generally inherit from in a rails project?
- The ApplicationController inherets from ActionController::Base, other controllrers generally inheret from ApplicationController.
4. How would I create a route if I wanted to see a specific horse in my routes fitle assuming I'm sticking to standard conventions and that I didn't want other CRUD functionality?
- In config/routes.rb:
```
resources :horses, only: [:show]
```
5. What rake task is useful when looking at routes, and what information does it give you?
- `rake routes`, which gives you all available route prefixes, HTTP verbs, URIs, and the corrsponding controller actions currently available in your app.
6. What is an example of a route helper? When would you use them?
- new_horse_path (prefix_path) is a route helper which can be used to find the path '/horses/new' without hard-coding the actual path.
7. What's the difference between what `_url` and `_path` return when combined with a routes prefix?
- `url` returns the full url (including the HTTP protocol and domain). In other words, `url` returns an absolute path, while `_path` returns a path relative to the root of the site.
8. What are strong params and why are the necessary?
- strong params prevent users from passing unwanted input params to the controller. Good for preventing hacking.
9. What role does `form_for` play in helping us create our forms?
- `form_for` is a rails helper that generates an HTML form for a given object passed to it.
10. How does `form_for` know where to submit the user's input?
- when you pass form_for an object, it checks to see if that object already exists. If it already exists, the input is submitted to the edit route. If not, it is submitted to the new route so that the object can then be created.
11. Create a form using a `form_for` helper to create a new `Horse`.
```
<%= form_for @horse do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :breed %>
<%= f.text_field :breed %>
<%= f.submit %>
<% end %>
```
12. Why do we want to validate our models?
- In validating our models we can check that all the necessary attributes/columns/fields are present (and/or unique and/or probably other things that we have not learned yet) to create an object. This helps ensure that our objects in the database are well-formed.
## Week Four Recap
* What is a cookie?
- A cookie is a way of identifying a particular user.
* What’s the difference between a session and a cookie?
- A session is an encrypted cookie.
* What’s a flash and when do you want to use flashes?
- A flash is a message used to give the user feedback on actions taken on the site. For example, a flash could be used to let a user know that a delete action was successful, or that the login password they used was incorrect.
* Why do people say “HTTP is stateless”?
- HTTP is stateless in that it does not "remember" or have records of your previous requests.
* What’s authentication? Explain.
- Authentication is used to confirm a user is who they say they are.
* What’s the difference between authentication and authorization?
- Authentication is used for identification, Authorization is used to determine what/where a user has access to on the site.
* What’s a before filter?
- A before filter calls a method _before_ any CRUD operations take place in that controller.
* How do we keep track of a user once they’ve logged in?
- We store their info in a cookie/session.
* When do you want to namespace a resource? When do you want to nest a resource? What's the differences between those two approaches?
- Namespacing is useful for creating custom uri patterns. Nesting resources makes sense to structurally represent relations between resources. For example, if a job belongs to a company, then it makes sense to nest jobs under companies. Namespacing does not represent relations in the same way.
* At a high level, what tools can you use to implement authorization? How would you use them?
- We can use the bcrypt gem, add has_secure_password to our user model, and add password_digest as a column in our database.
* What's an enum, and what advantages does it offer? What data type needs to be in your database to use an enum? Where do you declare an enum?
- An enum is declared in a model, and allows us to store attribute values as integers within the database, but query them by name.
* What are some strategies you can use to keep your views DRY?
- Use partials, move logic to POROs, create helper methods.
## Week Five
* What's the difference between Authentication and Authorization?
* Why are both necessary for securing our applications?
* What's a before_action filter in Rails?
* How can we scope a filter down to only work with specific actions?
* What's an enum attribute in ActiveRecord? Why would we ever want to use this?
* When thinking about Authorization, why might we want to namespace a resource?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment