Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save seeker105/274be5a621d2c3d3b57bf6c7197e48c1 to your computer and use it in GitHub Desktop.
Save seeker105/274be5a621d2c3d3b57bf6c7197e48c1 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 unigue key for table x, foreign key is putting x's primary key into another table to establish a relationship. The default name is 'id'. A foreign key can be in any table, usually in the 'many' of a one-to-many relationship. Naming convention is tablename_id.
#### Write down one example of:
* a `one-to-one `relationship.
Each person has one toothbrush
* a `one-to-many relationship`.
One person may own many books
* a `many-to-many relationship`.
Books and authors. An author can write more than one book, and a book can have more than one author.
What's the difference between test, development, and production databases?
The test database is used for testing. It is cleaned and reset with every test. The development database is used while designing the program. The production database holds info ready for inclusion with the finished/distributed/maketed product.
How do you create a Rails app from the command line with a postgres database?
Use the command `rails new` appname `-d postgresql`
What files are created by typing rails g model ...?
The 'g' stands for generate. It creates a migration, an empty author.rb model file, an empty author_test.rb file for testing setup, and an authors.yml file in fixtures for storing the database.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment