Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marinacor1/16b868a086e0cb72eb31df8700a082d3 to your computer and use it in GitHub Desktop.
Save marinacor1/16b868a086e0cb72eb31df8700a082d3 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 unique identifier within a table. Foreign key is the unique identifier of another table that has a relationship with the table.
By default it is the ID. A foreign key will be in the other table and will have a name that starts with the other table name (ex. students) and id.
So an example would be student_id.
#### Write down one example of:
* a `one-to-one `relationship. Person to sandwich. Bicycle to owner. A business to CEO.
* a `one-to-many relationship`. A school to student. A bank to loan.
* a `many-to-many relationship`. A student to course. A business to client.
What's the difference between test, development, and production databases?
Each database is the collection of tables that have been migrated into the application. There are three possible databases that can be created.
A test database is typically deleted after each run and then populated again at the beginning of the next run. Allowing for the cleanest and most accurate testing.
Development database is what is populated as you (developer in development stage) play around on the browser. If you add information to a table and post, it will be added to this development database.
This information can be deleted typically through a Delete process on your browser, same with update/ edit, etc.
A production database is the one that will be populated by the client. The client can specify how this information will be populated, but will typically be populated through the same process as the development, though information will be more relevant to the business and accurate (real information).
How do you create a Rails app from the command line with a postgres database?
rails new appname --database=postgresql
appname represents the actual application name and is different from above.
What files are created by typing rails g model ...?
It creates a db -> migrate file that includes a create_table with appropriate column names.
It also creates a models file with the table name like class Author
It creates the file but does not migrate.
What's the difference between typing rails g model ... and rails g migration ...?
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 model will create 4 files (2 test, model and migration)
rails g migration will create just the migration file
rails g migration RemoveColumnQuantity
(check migration file)
rake db:migrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment