Last active
May 27, 2020 09:03
-
-
Save Ollie-Boyd/85d2ab0b8c84840c5e0a83b2e50b8439 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # CRUD Quiz | |
| Use the solution to this afternoon's Property Tracker lab to answer the following questions. Please write your answers under each question, push the file to GitHub, and submit in the usual way. | |
| ## MVP Questions | |
| In our Property Tracker application: | |
| Q1. Where are we instantiating instances of the `Property` class? | |
| In the initialize function of property.rb which is called here for example: | |
| properties = results.map {|property_hash| Property.new(property_hash)} | |
| Q2. Where are we defining the SQL that enables us to save the ruby `Property` object into the database? | |
| Property class, function save, in the sql variable. | |
| Q3. In `console.rb`, which lines modify the database? | |
| Property.delete_all() | |
| property1.save() | |
| property2.save() | |
| property3.save() | |
| property1.delete() | |
| Q4. Why do we not define the id of a `Property` object at the point we instantiate it (‘new it up’)? | |
| The database will assign it an id. If we do it we'll mess things up. | |
| Q5. Where and how do we assign the id (that is generated by the database) to the ruby `Property` object? | |
| @id = db.exec_prepared("save", values)[0]["id"].to_i | |
| Q6. Why do we put a guard (an `if` clause) on the `@id` attribute in the constructor? | |
| Because we might instantiate an instance from outwith the database so it won't have an id assigned in the options hash when we pass it to initialize. | |
| Q7. Why are some of the CRUD actions represented by instance methods, and others by class methods? | |
| We don't always want to create an object to call them on when we don't need to. | |
| Q8. What type of data structure is returned by calls to `db.exec_prepared()`? In the `save` method, how do we access the id from the returned data structure? | |
| An array of hashes. | |
| Q9. Why do we use prepared statements when performing database operations? | |
| To prevent SQL injection. | |
| ## Extension Questions | |
| Look at the `find_by_id` and `find_by_address` methods in the `Property` class. | |
| Q10. What do they take in as their arguments? | |
| an id, an address. | |
| Q11. What are their return values? | |
| Objects of the property class. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment