Skip to content

Instantly share code, notes, and snippets.

@dcousette
Created April 13, 2015 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcousette/3c3c2c3ab3e157f382a4 to your computer and use it in GitHub Desktop.
Save dcousette/3c3c2c3ab3e157f382a4 to your computer and use it in GitHub Desktop.
Tealeaf Course 2 Quiz: Lesson 1
Rails Course 2 - Quiz: Lesson 1
1. They call it a relational database because it is used to create, edit, retrieve and store related sets of data.
2. SQL - stands for Structured Query Language, and it is the language used to manipulate a relational database.
3. The two predominate views into a database are: the data view, and the schema view. The data view displays a tables'
columns and the rows it contains. Similar to the view of spreadsheet. The schema view displays information about the
column names in the database and their associated data types.
4. The primary key.
5. A foreign key is a column in a DB table that references a primary key column that exists in another table.
It is used to estabish a "belongs_to" association between two sets of data - with the table containing
the foreign key "belonging" to the primary key table it references.
6. At a high level, the Active Record pattern performs Object Relational Mapping and translates
Object Oriented code into SQL that a DB can understand and execute. The pattern abstracts away
the need to write SQL code.
7. The table name should be "crazy_monkies".
8. Project will contain: has_many :issues
Issue will contain: belongs_to :project
The issues table will have a foreign_key of :project_id.
9. The other model would be an Animal class. The animals DB schema at least contains an :id, a :name,
and a :zoo_id.
Zoo now has access to these new methods: .animals, .animals=
new_zoo = Zoo.create(name:"San Diego Zoo")
jumpster = Animal.create(name:"jumpster" type:"kangaroo")
new_zoo.animals << jumpster
10. Mass assignment is setting multiple values for an object when it's created, done all in one line.
Non-mass assignment would be creating an object, then setting its attributes on subsequent lines of code.
11. This would return the first object or row in the Animal table.
12. Animal.create(name:"Joe") - this would save to DB. Using .new would require you to .save as well.
13. At the DB level a M:M association works by creating a third join table and placing a foreign_key to
both existing tables on it. Additional declarations are made in the models.
14. The two ways to support M:M associations at the model level are: "has_and_belongs_to_many" & "has_many through:".
HABTM- Pros: This association doesn't require a 3rd join table or model.
Cons: Not as flexible as "has_many_through", you can't add other attributes (because there is no join table used)
HMT- Pros: More flexible, can associate more attributes on the join table
Cons: You have to add another table and model.
15. Assuming DBs have been set-up properly. In the models we'd do the following:
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 :user
belongs_to :group
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment