Skip to content

Instantly share code, notes, and snippets.

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

Primary key is an identifier that is used to identify an item/object/thing/row in that table. A foreign key is the primary key of another table.

A primary key is unique in its table. A foreign key does not need to be unique.

Where would we find a primary key?

In a column of a table where it is being used to identify the item held in that table.

What would it be called by default?

ID

Where would we find a foreign key?

On a related table.

What is the naming convention for a foreign key?

table_name_id

Write down one example of:

  • a one-to-one relationship. Spouse to Spouse. Marriage to Divorce. Person/Social Security Number.
  • a one-to-many relationship. House/Occupants. School/Students.
  • a many-to-many relationship. Musicans/Albums, Student/Classes, Videogames/Players

Rails Stuff

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

Test is used when tests are run by setting the environment variable to test. Development is used locally on your machine outside of tests. Production is used on a server in a production environment.

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

rails new some_project_name

Optional arguments: --database=postgresql

What files are created by typing rails g model ...?

rails g model is equal to rails generate model, it takes an argument of the model that you'd like to generate, along with optional arguments regarding the data that you'd like to store in the model (e.g. first_name:text last_name:text, etc.)

It creates a new migration, a model, a model test, and a fixture.

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