Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jwashke/7a50ee5dcf46d5fb2a4879dd85a03a32 to your computer and use it in GitHub Desktop.
Save jwashke/7a50ee5dcf46d5fb2a4879dd85a03a32 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?

A primary key is the primary unique identifier of a table, a foreign key is the primary key of another table that serves as as the relationship between the two tables. A primary key is generally named id, a foreign key is named othertable_id whith the name of the table the foriegn key links to followed by id.

Write down one example of:

  • a one-to-one relationship.
    • Each car has one license plate and each license plate belongs to one car
  • a one-to-many relationship.
    • A School has many students but each student has only one school
  • a many-to-many relationship.
    • A teacher has many students a student has many teachers

What's the difference between test, development, and production databases?

test databases are used only for your test, development databases for while you are in development of your app, and production as your live deployed database

How do you create a Rails app from the command line with a postgres database?

rails new app_name --database=postgresql

What files were created by running rails g model

It made our model file, our migration file with the table being created with all columns specified by arguments. It also generated a model test and fixtures file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment