Skip to content

Instantly share code, notes, and snippets.

@kangkyu
Last active August 29, 2015 14:02
Show Gist options
  • Save kangkyu/2853ad5d9f549cd1982b to your computer and use it in GitHub Desktop.
Save kangkyu/2853ad5d9f549cd1982b to your computer and use it in GitHub Desktop.
tealeaf academy course 2 Quiz 1
1. why do they call it a relational database?
because they have join and association between tables.
(solution: because the tables within the database are associated with each other. This association can be created with primary and foreign keys and various syntax.)
2. what is SQL?
language used in all database systems
(solution: SQL stands for "structured query language" and it is used to manage operations of a relational database.)
3. two predominant views into a relational database…
?
(solution: the two predominant views are the data view and the schema view. a 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 nameds 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?
"primary key"
5. What is a foreign key, and how is it used?
integer field associated to primary key of another table, used for table join
(solution: a foreign key is the identifier that connects an association with the models involved. the foreign key is always on the "many" side of association 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.
?
(solution: ActiveRecord is a way to access the database. A database 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?
“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
class Issue # belongs_to :project, foreign_key: 'project_id'
9. Given this code...
class Zoo < ActiveRecord::Base
has_many :animals
end
- what do you expect the other model to be and what does database schema look like?
class Animal < ActiveRecord::Base
belongs_to :zoo, foreign_key: 'zoo_id'
end
(solution: database schema wil have tables named zoos and animals. zoos table with a primary key of id, animals table with a primary key of id and foreign key of zoo_id)
- what are the methods that are now available to a zoo to call related to animals?
zoo.animals
zoo.animals.first
(solution: you can also iterate through the list of a zoo's animals and display certain properties of the animals.)
- how do I create an animal called "jumpster" in a zoo called "San Diego Zoo"?
Zoo.create(name: "San Diego Zoo").animals << Animal.create(name: "jumpster")
10. what is a mass assignment? what is the non-mass assignment way of setting values?
- use create method and put each value of one or more attributes.
- for non-mass, use assignment operator ("=") for an attribute at a time, and then save the value assigned for the variable.
(solution: mass assignment allows us to assign multiple values to attributes with one single assignment method.)
11. what does this code do? Animal.first
find the first row of animals table
(solution: 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?
- Animal.create(name: "Joe"), create method saves to the database, or
- joe = Animal.new # joe.name = "Joe" # joe.save
13. how does a M:M association work at the database level?
make two 1:M association for the two tables to a join table only has foreign keys for them.
(solution: 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?
has_many through
has_and_belongs_to_many
(solution: 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, but it is less flexible and we cannot add additional attributes to the join table.)
15. suppose we have models "User" and "Group", then we have a M:M association all set up. how do we associate the two?
class User < ActiveRecord::Base
has_many :groups, through: :assignments
has_many :assignments
end
class Group < ActiveRecord::Base
has_many :users, through: :assignments
has_many :assignments
end
class Assignment < ActiveRecord::Base
belongs_to :user, foreign_key: 'user_id'
belongs_to :group, foreign_key: 'group_id'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment