Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save theonlyrao/c553d207c8a271351eeb71ee4fcbcd93 to your computer and use it in GitHub Desktop.
Save theonlyrao/c553d207c8a271351eeb71ee4fcbcd93 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 a unique identifier in Table. It can exist as a foreign key in AnotherTable or other tables with which it has a relationship. "id" - default name. "table_id" in AnotherTable.
#### Write down one example of:
* a `one-to-one `relationship.
(in the contemporary West), one husband has one wife
* a `one-to-many relationship`.
one person owns many glasses
one mother has many children
* a `many-to-many relationship`.
multiple people live in multiple historical home addresses
multiple people have multiple jobs
####What's the difference between test, development, and production database?####
The difference between these databases is that whether they are available to be operated on by the models depends on the environment from which the method trying to access the database is called. The environment is set by the object that calls the method that accesses the database.
####How do you create a Rails app from the command line with a postgres database?####
`rails new practice_application_1602 --database=postgresql`
####What are the files created by typing `rails g model ... `?####
`rails g model ... ` is short for `rails generate model ...`. It is a command telling rails to create the migration file necessary to create a table specified in the first arg of that command, with attributes specified in the second + args in the command. The command also creating a test file for the class associated with the table and a test database in fixtures. It is still necessary to run the migration files in order to create the table and associated attibutes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment