Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Claudia108/5ad4a673d1eef08930581ced450d06ef to your computer and use it in GitHub Desktop.
Save Claudia108/5ad4a673d1eef08930581ced450d06ef 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 unique identifier of entries in table (row). In a one to many relationship the primary key of the one becomes foreign key of many - table. Default name of primary key is id. As the foreign key it is called "tablename_id".
#### Write down one example of:
* a `one-to-one `relationship. One person owns one bycicle, shared attributes: bike-rack, bike-pump.
* a `one-to-many relationship`. One person has many clothes.
* a `many-to-many relationship`. Rentals-to-Customers
#### What's the difference between test, development, and production databases?
test database is used in testing, cleaned after tests are run, development for designing the program, production included in shipping of final one. Different environments for different stages of the development to control variables. We don't want to change anything in production database. Changes happen in test and developmet databases.
#### 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 ...?
4 files: model, migration, model_test, yml_file. g stands for generate. Migration file is fully built.
#### Imagine that you have a table students. What is the ActiveRecord query that would return all students with the first name of Richard?
Student.pluck(name: "Richard")
How would you update the student record with ID 4 to have a new phone number of "101-222-3333"?
Student.where(id: 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