Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alirezaandersen/de7debb8e1bbd18bdceb38c05c0e1a3e to your computer and use it in GitHub Desktop.
Save alirezaandersen/de7debb8e1bbd18bdceb38c05c0e1a3e 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 main key that determines the identity or uniqueness of the entry in a table Foreign key is the primary key for another table that is directly related to the current table

Write down one example of:

  • a one-to-one relationship.

  • spouse-to-spouse

  • marriage-to-divorce

  • person-to-ssn

  • a one-to-many relationship.

  • mother-to-childern

  • fridge-to-food

  • turing-to-student

  • landlord to tenant

  • a many-to-many relationship.

  • recipe-to-ingredients

  • musicians-to-albums

  • restaurant-server-to-restaurant-patriots

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

  • development database is used for the building of an specific part of the application
  • production database is a process that you would not want to modify because by this stage most databases have valuable content that you would not want to get rid of.
  • test database is used for testing a specific state of a application.
  • How do you create a Rails app from the command line with a postgres database?
  • go to the folder in which you would like your project in and type the following rails new <project_name> --database=postgresql
  • What files are created by typing rails g model ...?
  • the g stands for generate, rails generate model Author first_name:text last_name:text so this is broken down by rails (generating a model) based on a Author in which the Author has to columns to keep track of the first_name and last_name the : represent the type of content in this example it is a text.
    The result is that this command creates a model, migrate, yaml database, and test files.
  • What is the difference between rails g model ... and rails g migration...?
    • rails g model allows the user to generate model, a migration, and two files related to testing.
    • rails g migration creates the migration and models seperately

*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 RemoveQuantityColumninItems 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?

    • Students.find(first_name: "Richard"
  • How would you update the student record with ID 4 to have a new phone number of "101-222-3333"?

    • Students.find_by(id: ID4).update(phone_number:"101-222-3333")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment