What is the difference between a primary key and a foreign key?
- Primary Key = Usually an autoincrementing number for each row in the database, specific to the row.
- Foreign Key = Key in a database which comes from another table, doesnt have to be that other table's primary key but often is. Where would we find a primary key?
- Primary key is usually the first column in a table, autoincrementing # called ID What would it be called by default?
- ID Where would we find a foreign key?
- Secondary table, coming from another (usually primary) table What is the naming convention for a foreign key?
- 'something_id' where something is singular form of noun, comes from other table being referenced. One-to-One Relationship Example:
- Person / SSN One-to-Many Relationship Example:
- Child / Toys Many-To_many Relationship Example:
- Students / Classes 3 Different Environments:
- Test : Used during testing only, wiped clean each run of tests
- Development : Used while writing your app, locally run server and database
- Production : Real data on live app What files are created by typing rails g model ...?
- Migration File
- Model File
- 2 Test Files (Model and Fixtures) What's the difference between typing rails g model ... and rails g migration ...?
- Model will generate migration file and model
- Migration will not generate a model with the same name as the table, but will generate migration file 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?
- rails g migration RemoveQuantityFromItems quantity:integer, in terminal 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")
- or: Student.update(4, phone_number: "101-222-3333")