Created
December 4, 2013 18:48
-
-
Save mattdvhope/7793208 to your computer and use it in GitHub Desktop.
Tealeaf quiz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. They're called relational databases because the collection of tables within them are related via primary keys and foreign keys. | |
2. SQL is 'Standard (or Structured) Query Language.' It is a language used by database applications (or the SQLite file). | |
3. The two predominant views into a relational database are schema view (which shows the column names) and data view (which shows the data in each row in under the columns--and also shows the column names). | |
4. The primary key (column). | |
5. A foreign key is in the foreign key column on the table which is on the 'many' side of a 1:M relationship (relationship between two tables). It is the same as & corresponds to the primary key of the 'one side' and is used to create the relationship between the two tables. | |
6. The ActiveRecord pattern enables a database table to be wrapped in a class and for a database row to be wrapped into an object. | |
7. "crazy_monkeys" | |
8. The foreign key will be project_id and it will be in the 'issues' table. The model associations will be: | |
class Project < ActiveRecord::Base | |
has_many :issues | |
end | |
class Issue < ActiveRecord::Base | |
belongs_to :project | |
end | |
9. The other model is: | |
class Animal < ActiveRecord::Base | |
belongs_to :zoo | |
end | |
- The schema is: | |
class CreateZoos < ActiveRecord::Migration | |
def change | |
create_table :zoos do |t| | |
t.string :name | |
t.string :address | |
t.timestamps | |
end | |
end | |
end | |
class CreateAnimals < ActiveRecord::Migration | |
def change | |
create_table :animals do |t| | |
t.string :animalname | |
t.integer :zoo_id | |
t.timestamps | |
end | |
end | |
end | |
- Methods available to zoo: animals (both getter & setter) | |
- Use mass-assignment in the rails console to create a "jumpster" animal/object... | |
> jumpster = Animal.create(animalname: "jumpster", zoo_id: 1) | |
10. It is assigning multiple values to multiples keys at the same time. Here is an example of non-mass assignment: | |
> jumpster = Animal.create | |
> jumpster.animalname = "jumpster" | |
> jumpster.zoo_id = 1 | |
11. It returns the first row of the 'animals' table in a ruby object format: | |
#<User id: 1, animalname: "jumpster", zoo_id: 1, created_at: "2013-12-02 20:58:24", updated_at: "2013-12-02 20:58:24"> | |
12. In the rails console: | |
> Animal.create(name: "Joe") | |
- The .create class method will save it in the database. | |
- The .new class method will not save it in the database, but if .save method is used afterwards on that object, it will then be saved into the database as a new row. | |
13. There must a join table created between the two tables. For example, students have many courses and courses have many students. A join table called 'course_students' would contain the columns, 'course_id' and 'student_id'. | |
14. The two way to support M:M associations are: | |
- has_many_and_belongs_to -- it does not need a separate model, but in inflexible for future iterations with the join table. | |
- has_many, through -- requires a separate model for the join table, but allows for flexibility for future iterations with the join table. | |
15. Models... | |
class User < ActiveRecord::Base | |
has_many :user_groups | |
has_many :groups, through: :user_groups | |
end | |
class Group < ActiveRecord::Base | |
has_many :user_groups | |
has_many :users, through: :user_groups | |
end | |
class UserGroup < ActiveRecord::Base | |
belongs_to :users | |
belongs_to :groups | |
end | |
...and migrations.... | |
class CreateUsers < ActiveRecord::Migration | |
def change | |
create_table :users do |t| | |
t.string :usename | |
t.timestamps | |
end | |
end | |
end | |
class CreateGroups < ActiveRecord::Migration | |
def change | |
create_table :groups do |t| | |
t.string :groupname | |
t.timestamps | |
end | |
end | |
end | |
class CreateUserGroups < ActiveRecord::Migration | |
def change | |
create_table :user_groups do |t| | |
t.integer :user_id | |
t.integer :group_id | |
t.timestamps | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment