Skip to content

Instantly share code, notes, and snippets.

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

  • a primary key is the id of the current table, a foreign key is the id of a different table

Where would we find a primary key?

  • in the id field of the table

What would it be called by default?

  • id

Where would we find a foreign key?

  • in the other_table_id field

What is the naming convention for a foreign key?

  • name_of_table_id

Jon L and Sunny wooooooooo

Write down one example of:

  • a one-to-one relationship -> secretaries to bosses
  • a one-to-many relationship -> person to shirts
  • a many-to-many relationship -> students to classes

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

  • test database - the database used to run tests on. usually empty or populated with minimal data
  • dev database - the one that's used when you're working on changing features and working on the app
  • production database - the one that get's used in the current public/publishable version of the app

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

  • rails new [project name] --database=postgresql

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

  • rails creates all the necessary migration and model files for the model that you specify

What's the difference between typing rails g model ... and rails g migration ...?

  • rails g model will create all the things you need, model + migration + other files
  • rails g migration will create just the 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?

  • rake db:rolback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment