Answer these quizzes, then updated after watching solution.

##1. Why do they call it a relational database?

Relational database can store the relationship between data and data. For example, a table store customer’s info, and a table store consuming record, these two table can be associated with another table, to establish the customer’s consuming history. There are relationships between data and data in relational database. ex. sqlite, MySQL, PostgreSQL ect.

the tables within the database are associated with each other. This association can be created with primary/foreign keys and various syntax.

##2. What is SQL?

SQL(Structured Query Language), it’s domain specific language to manipulate database.

##3. There are two predominant views into a relational database. What are they, and how are they different?

There are two associations concepts in relational database:

  • 1:M
    One to Many makes target object can associate with many other objects, like one customer can have many consuming records, we can track the customer’s consuming history with this association or we can track the record is generated by which customer.

  • M:M
    Many to Many makes target objects can associate with many other objects, like posts can be assign to many categories, and category can assign to many poasts. We also can track target post or target category is assigned to which one.

The two predominant views are the data and schema views.
Data view displays like a spreadsheet, with the table columns at the top and rows of data per each object instance.
A schema view shows us the column names and the value type of each column.

##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.

Attributes. Column indicate that what the row values mean.

We call this the “primary key”.

##5. What is a foreign key, and how is it used?

Every row data in a table is included a primary key. If we want to associated with other table, we need this primary key to store which row data we wanna to be connected with this one. The store primary key value’s column is called foreign key.

A foreign key is the identifier that connects an association with the models involved. The foreign key is always on the “many” side and is always in an integer type.

##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.

ActiveRecord is mapping the data operation in tables. It substitutes SQL operation and using migration files to define & operate table column.

ActiveRecord is a way to access the database. A database table is related to a class. An object of that class is created as a row in the table. This object can have different attribute values shown as the columns in the table. We can create, retrieve, update, and delete the object instances by altering the database table.

##7. If there’s an ActiveRecord model called “CrazyMonkey”, what should the table name be?

If I’m confused with this, I can use rails console mode, to run

1
"CrazyMonkey".tableize

and the answer should be crazy_monkeys.

##8. If I’m building a 1:M association between Project and Issue, what will the model associations and foreign key be?

in model files

1
2
3
4
5
6
7
class Project ActiveRecord::Base
has_many :issues
end

class Issue ActiveRecord::Base
belongs_to :project
end

in migration files

1
2
3
4
5
6
class CreateIssue ActiveRecord:Migration
def change
create_table :issues do |t|
t.integer :project_id
end
end

##9. Given this code

1
2
3
class Zoo < ActiveRecord::Base
has_many :animals
end
  • What do you expect the other model to be and what does database schema look like?

    There should be antoher class named “Animal”, and it belongs to Zoo. Animal should at least have name, and a foriegn key to set to zoo id.

1
2
3
class Animal < ActiveRecord::Base
belongs_to :zoo
end
1
2
3
4
5
6
class CreateAnimal ActiveRecord:Migration
def change
create_table :animals do |t|
t.integer :zoo_id
end
end
  • What are the methods that are now available to a zoo to call related to animals?

    1
    zoo.animals
  • How do I create an animal called “jumpster” in a zoo called “San Diego Zoo”?

    If in rails console mode:

    1
    2
    3
    $ zoo = Zoo.create(name: "San Diego Zoo")
    $ animal = Animal.create(name: "jumpster")
    $ zoo.animals << animal

##10. What is mass assignment? What’s the non-mass assignment way of setting values?

With mass assignment, we can do

assumption fields: [title, body]

1
@post = Post.new(params[:post])

without mass assignment, we need to do this

1
2
3
@post = Post.new
post.title = "post title"
post.body = "post body"

##11. What does this code do? Animal.first

Query the first data in table Animal.

This will return the first row of data for the first Animal instance object in the animals table.

##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?

This will make sure it saves to database

1
Animal.create(name: "Joe")

And this will work too

1
2
animal = Animal.new(name: "Joe")
animal.save

##13. How does a M:M association work at the database level?

There is a join table between two relational tables, and it needs these two tables row data primary key to be its foreign key. For example, there is a join table called post_categories, and it stores two foriegn key named: post_id and category_id.

On the database level of a M:M association, we use a join table to support it. Both of the primary models will each have a 1:M association with the join table.
By using the has_many :through technique, we are able to create an indirect M:M association with the two primary models.

##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
    Chris metioned that this method is deprecated. If you don’t need to operate the join table, this method is easy to set up association between two tables. And it’s doing a more implicit way.

  • has_many through:
    You have to define has_many relation with join table, also define has_many through: realtion to both target table and join table. This explicit the way to connect target table and join table. It’s much easier to understand all realtionship between tables.

has_many :through requires an explicit join model and a join table, but it is more flexible and we can add additional attributes to the join table.
has_and_belongs_to_many doesn’t require a join model or a join table, but it is less flexible and we cannot add additional attributes to the join table.

##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?

User.rb
1
2
3
4
class User < ActiveRecord::Base
has_many :user_groups
has_many :groups, through: user_groups
end
Group.rb
1
2
3
4
class Group < ActiveRecord::Base
has_many :user_groups
has_many :users, through: :user_groups
end
UserGroup.rb
1
2
3
4
class UserGroup < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
create_user_groups.rb
1
2
3
4
5
6
7
8
class CreateUserGroups < ActiveRecord::Migration
def change
create_table :post_categories do |t|
t.integer :user_id
t.integer :group_id
end
end
end