Skip to content

Instantly share code, notes, and snippets.

@kafaichoi
Last active August 29, 2015 14:01
Show Gist options
  • Save kafaichoi/7568eb9235ea6cb15760 to your computer and use it in GitHub Desktop.
Save kafaichoi/7568eb9235ea6cb15760 to your computer and use it in GitHub Desktop.
My answer for the quiz in the lesson 1 of Rails
  1. Why do they call it a relational database? Relational database are usually consisted of different tables which are related to other table by foreign key. And Foreign key in one table is the primary key of a record in another table. We can easily make a query base on different criteria on different table by joining the table.

2.What is SQL? SQL stands for Structured Query Language. It is a Domain Specific Language for managing data in RDBMS

  1. There are two predominant views into a relational database. What are they, and how are they different? They are the data and schema views. Data view displays like a spreadsheet. A schema view descripts the column names and the value type of each column.

  2. 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. It is primary key.

  3. What is a foreign key, and how is it used? Foreign key is used for linking the relation between the record in its own table and the . The foreign key of a table will be the primary key of a record in other table. It can be used to identify/link a record in the related table.

  4. 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. In ActiveRecord model, each row of data in the database is connected to an object in the model.

7.If there's an ActiveRecord model called "CrazyMonkey", what should the table name be? The name should be crazy_monkeys and in fact we can check it by using method tableize in rails console.

  1. If I'm building a 1:M association between Project and Issue, what will the model associations and foreign key be? belongs_to : issues foreign_key: issue_id

  2. Given this code

has_many :animals
end``` 
What do you expect the other model to be and what does database schema look like?
zoos table will have primary key while animals table will have its primary key and foreign key zoo_id

What are the methods that are now available to a zoo to call related to animals?
A lot. For example, zoo.animals which return the array of object of animals in the instance zoo.

How do I create an animal called "jumpster" in a zoo called "San Diego Zoo"?
Animal.create(name:"jumpster",zoo_id:1) ## assume the zoo with name "San Diego Zoo" is the first instance.


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

e.g. 
```Animals.create(species:"Dog", age:8)
dog1 = Animals.create
dog1.species="Dog"
dog1.age=8
dog1.save```

11. What does this code do? Animal.first
It find the instance of Animal with animal_id = 1 (first row) in the table animals.
 
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?
animals.save

13. How does a M:M association work at the database level?
There are two ways including has_and_belongs_to_many and has_many :through. Both of them use a Join Table to connect each other. However we can only able to create indirect association between two table if we are usign has_many :through.

14. What are the two ways to support a M:M association at the ActiveRecord model level? Pros and cons of each approach?
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 to create a join model explicitly. However we cannot alter the model and therefore not able to add additional attributes to the join model.

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 < ActionRecord::Base
has_many: user_groups
has_many: groups ,through: :user_groups
end```

```class UserGroup < ActionRecord::Base
belongs_to: user
belongs_to: group
end```

```class Group < ActionRecord::Base
has_many: user_groups
has_many: users, through: :user_groups
end```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment