Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save deborahleehamel/2b139b9d0527c2e6d5ffeb8c21edfddc to your computer and use it in GitHub Desktop.
Save deborahleehamel/2b139b9d0527c2e6d5ffeb8c21edfddc to your computer and use it in GitHub Desktop.
## Models, Databases, Relationships in Rails
#### 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?
Primary key is the original id generated when a table is created. <br />
Foreign key is the relationship to another object in another table and is formatted with ending _id.<br />
#### Write down one example of:
* a `one-to-one `relationship.
one person has one social security number<br />
one social security number is assigned to one person<br />
Person-to-SSN<br />
Person to a current-country-passport<br />
Spouse-to-spouse<br />
Marriage-to-Divorce<br /><br />
* a `one-to-many relationship`.
Turing school has many students<br />
one student goes to one Turing school<br />
Person-to-sandwich<br />
Mother-to_children<br />
Fridge-to-food<br /><br />
* a `many-to-many relationship`.
students have many teachers<br />
teachers have many students<br />
Recipe-to-ingredients<br />
Musicians-to-albums<br />
Student-to-Instructors<br />
GroceryStores-to-Customers<br />
Hotel-to-TheirAmenities<br />
Rentals-to-Customers<br />
<br />
#### What's the difference between test, development, and production databases?
test database - used in testing and gets wiped clean after every test in separate test environment<br />
development database - when building application - creates separate environments for development<br />
production database - environment where you do not want to delete or make changes<br /><br />
#### How do you create a Rails app from the command line with a postgres database?
rails new name_of_application --database=postgresql<br />
rails new name_of_application -d=postgresql<br /><br />
#### What files are created by typing rails g model ...?
creates 4 things:<br />
migration to create table - in db/migrate a new migration file is created 20160405160217_create_authors.rb<br />
model file corresponding to table - in app/models a new model file is created author.rb<br />
test file for new model - in test/models/author_test.rb<br />
test fixture - in test/fixtures/author.yml<br /><br />
####What's the difference between typing rails g model ...
creates both the model and migration and creates a model
and rails 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?
rails g 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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment