Last active
August 29, 2015 14:17
-
-
Save madstap/a926da9a2237778720d6 to your computer and use it in GitHub Desktop.
Quiz for Tealeaf course 2 lesson 1
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
# Quiz: Lesson 1 | |
# Quiz from lesson one materials. You can do this yourself, or create a new gist | |
# or blog entry, answer the quiz, and post the link to the forums and others can | |
# take a look. | |
# 1. Why do they call it a relational database? | |
# Because it stores data and the relations between the data. | |
# 2. What is SQL? | |
# Structured Query Language, the language of relational databases. It is used to | |
# retrieve, change, make or delete data. | |
# 3. There are two predominant views into a relational database. What are they, | |
# and how are they different? | |
# Data and Schema views | |
# Schema shows the tables and columns and how they are related. | |
# Data shows the rows and the data in them. | |
# 4. In a table, what do we call the column that serves as the main identifier | |
# for a row of data? We're looking for the general database term, not the column | |
# name. | |
# The primary key. | |
# 5. What is a foreign key, and how is it used? | |
# A foreign key is the the primary id of another table, signifying a | |
# relationship between the current table and the other table. | |
# 6. At a high level, describe the ActiveRecord pattern. This has nothing to do | |
# with Rails, but the actual pattern that ActiveRecord uses to perform its ORM | |
# duties. | |
# It makes a class for each table in the database and wraps each row in an | |
# instance of that class, making setters and getters for each column. | |
# 7. If there's an ActiveRecord model called "CrazyMonkey", what should the | |
# table name be? | |
"CrazyMonkey".tableize | |
#=> "crazy_monkeys" | |
# 8. If I'm building a 1:M association between Project and Issue, what will the | |
# model associations and foreign key be? | |
class Project | |
has_many :issues | |
end | |
class Issue | |
belongs_to :project | |
end | |
class AddForeignKeyToIssues < ActiveRecord::Migration | |
def change | |
add_column :issues, :project_id, :integer | |
end | |
end | |
# 9. Given this code: | |
class Zoo < ActiveRecord::Base | |
has_many :animals | |
end | |
# 9.1. What do you expect the other model to be and what does database schema | |
# look like? | |
class Animal < ActiveRecord::Base | |
belongs_to :zoo | |
end | |
ActiveRecord::Schema.define(version: 20150317183630) do | |
create_table "zoos", force: :cascade do |t| | |
t.string "name" | |
t.datetime "created_at", null: false | |
t.datetime "updated_at", null: false | |
end | |
create_table "animals", force: :cascade do |t| | |
t.string "name" | |
t.string "species" | |
t.datetime "created_at", null: false | |
t.datetime "updated_at", null: false | |
t.integer "zoo_id" | |
end | |
end | |
# 9.2. What are the methods that are now available to a zoo to call related to | |
# animals? | |
zoo.animals | |
# => An 'array like thing' of all the animals for this zoo sorted by primary key | |
# Can use most (all?) methods that you can use on an array | |
# 9.3. How do I create an animal called "jumpster" in a zoo called "San Diego | |
# Zoo"? | |
zoo = Zoo.find_by(name: "San Diego Zoo") | |
zoo.animals << Animal.create(name: "jumpster", species: "kangaroo") | |
# 10. What is mass assignment? What's the non-mass assignment way of setting | |
# values? | |
# Mass assignment | |
jumpster = Animal.new(name: "jumpster", species: "kangaroo") | |
# Non-mass assignment | |
jumpster = Animal.new | |
jumpster.name = 'jumpster' | |
jumpster.species = 'kangaroo' | |
# 11. What does this code do? | |
Animal.first | |
# Returns the first row of the animals table as an instance of the class Animal. | |
# 12. If I have a table called "animals" with columns called "name", and a model | |
# called Animal, how do I instantiate an animal object with name set to "Joe". | |
# Which methods makes sure it saves to the database? | |
joe = Animal.new(name: 'Joe') # Instantiates the object in memory. | |
joe.save # Saves it to the database. | |
joe = Animal.create(name: 'joe') # Does both of the above. | |
# 13. How does a M:M association work at the database level? | |
# It works by creating a join table with each row being the primary keys of the | |
# tables to be associated. | |
# 14. What are the two ways to support a M:M association at the ActiveRecord | |
# model level? Pros and cons of each approach? | |
has_and_belongs_to_many | |
# The join table has no model, the models to be associated just expects a table | |
# to exist in the database, named like: | |
[table1_name, table2_name].sort.join('_') | |
# The drawback of this is less flexibility, the join table can't behave in any | |
# specific way. The upside of this way is that you don't have to make a model. | |
has_many :through | |
# Make a model of the join table. The drawback is a little more typing. The | |
# upside is that you can add constraints and behaviors to the association | |
# itself. | |
# 15. Suppose we have a User model and a Group model, and we have a M:M | |
# association all set up. How do we associate the two? | |
class User | |
has_many :group_users | |
has_many :groups, through: :group_users | |
end | |
class Group | |
has_many :group_users | |
has_many :users, through: :group_users | |
end | |
class GroupUsers | |
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