Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kamiboers/301e5a994d86143bf9e582fb357c6da8 to your computer and use it in GitHub Desktop.
Save kamiboers/301e5a994d86143bf9e582fb357c6da8 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 unique primary key exists for every element/row in a table, and would be called that object's id. A foreign key is the identifier of an object/row in a table different from the one in which it is found, and does not have to be unique in the table in which it is found. It points to a related object. The naming convention is the table/object name + _ + id.
#### Write down one example of:
* a `one-to-one `relationship: student to locker assignment
* a `one-to-many relationship`: teacher to students
* a `many-to-many relationship`: students to classes
#### What's the difference between test, development, and production databases?
Test databases are used and destroyed for testing purposes, development databases are created as part of the process of developing interactions, etc., and production databases are used when the project is live and accessed by the outside world.
#### How do you create a Rails app from the command line with a postgres database?
rails new project_name --database=postgresql
#### What files are generated by typing 'rails g model ...'?
a model file, a migration file, a yaml file (we delete this) and a test file.
#### What's the difference between typing rails g model ... and rails g migration ...?
rails g model will generate a model and a migration, while rails g migration creates only a migration file, and can be used in combination with migration actions.
#### 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?
You can edit the migration file to remove the attribute, if only you have access to the files. Or you can create a new migration file with a method to 'remove column :items, :quantity
#### 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')
#### How would you update the student record with ID 4 to have a new phone number of "101-222-3333"?
student = Student.find(4)
student.update_attribute :phone_number, "101-222-3333"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment