Skip to content

Instantly share code, notes, and snippets.

@ToniRib
Last active December 15, 2015 19:05
Show Gist options
  • Save ToniRib/9a506832f096c3ce8935 to your computer and use it in GitHub Desktop.
Save ToniRib/9a506832f096c3ce8935 to your computer and use it in GitHub Desktop.
Models, Databases, and Relationships

Models, Databases, and Relationships

  1. 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 a unique identifier for records in a database table. A foreign key is a column in a table that references the primary key from a different table. A primary key is usually found in the first column of a database table, and by default is called just 'ID.' A foreign key could be in any column of a table, but is usually an integer referencing the primary key of a different table. The naming convention for a foriegn key is "_id" where is not plural. Foreign keys are used to link data in one table to data in another table.
  1. Write down one example of a one-to-one relationship.
  • A person has one registered username for a site
  1. Write down two examples of a one-to-many relationship.
  • I have three pets
  • I have many books
  1. Write down two examples of a many-to-many relationship.
  • A movie has many show times, and the same show time may have many movies
  • I have many mentors (and my mentors have many students)
  1. What's the difference between test, development, and production databases?
  • test: used for tests only, gets cleaned out after every single test
  • dev: what you use when building your app, opens in localhost
  • production: what is actually deployed to the web and used by real users
  1. How do you create a Rails app from the command line with a postgres database?
rails new application_name --database=postgresql
  1. What files are created by typing rails g model ...?
  • the migration to create the table
  • the model file corresponding to the table
  • a test file for the new model
  • a text fixture (which is a yaml file)
  1. What's the difference between typing rails g model ... and rails g migration ...?
  • rails g model creates both the model and the migration while rails g migration creates only the migration (and no model)
  1. 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?
  • To generate the migration rails generate migration RemoveQuantityFromItems quantity:integer
  1. 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')
  1. 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