- What is the difference between a primary key and a foreign key? Where would we find a primary key? What would it be called by default? Where would we find a foreign key? What is the naming convention for a foreign key?
- A primary key is a unique identifier for records in a database table. A foreign key is a column in a table that references the primary key from a different table. A primary key is usually found in the first column of a database table, and by default is called just 'ID.' A foreign key could be in any column of a table, but is usually an integer referencing the primary key of a different table. The naming convention for a foriegn key is "_id" where is not plural. Foreign keys are used to link data in one table to data in another table.
- Write down one example of a one-to-one relationship.
- A person has one registered username for a site
- Write down two examples of a one-to-many relationship.
- I have three pets
- I have many books
- Write down two examples of a many-to-many relationship.
- A movie has many show times, and the same show time may have many movies
- I have many mentors (and my mentors have many students)
- What's the difference between test, development, and production databases?
- test: used for tests only, gets cleaned out after every single test
- dev: what you use when building your app, opens in localhost
- production: what is actually deployed to the web and used by real users
- How do you create a Rails app from the command line with a postgres database?
rails new application_name --database=postgresql
- What files are created by typing rails g model ...?
- the migration to create the table
- the model file corresponding to the table
- a test file for the new model
- a text fixture (which is a yaml file)
- What's the difference between typing rails g model ... and rails g migration ...?
rails g model
creates both the model and the migration whilerails g migration
creates only the migration (and no model)
- Imagine that the items table has a category called quantity. What command would you type if you wanted to get rid of the quantity attribute?
- To generate the migration
rails generate migration RemoveQuantityFromItems quantity:integer
- Imagine that you have a table students. What is the ActiveRecord query that would return all students with the first name of Richard?
Student.where(first_name: 'Richard')
- How would you update the student record with ID 4 to have a new phone number of "101-222-3333"?
Student.find(4).update(phone_number: '101-222-3333'