Skip to content

Instantly share code, notes, and snippets.

@Sessl
Last active December 27, 2015 05:28
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 Sessl/7273855 to your computer and use it in GitHub Desktop.
Save Sessl/7273855 to your computer and use it in GitHub Desktop.
Rapid Prototyping with Ruby on Rails - Week 1 Quiz
1. Why do they call it a relational database?
It is a collection of tables of data items which are created according to the relational model.
In the relational model a table is called a relation, which is a set of tuples (rows) that have
the same attributes /columns (data structure). A tuple represents an object and information about it.
The domain and constraints of an attribute apply to all data that are referenced by that attribute.
Data are stored and accessed through relations.
2. What is SQL?
It stands for Structured Query Language.
It is a programming language for managing data stored in Relational Database
Management Systems (RDBMS).
3. There are two predominant views into a relational database. What are they, and how are they different?
1 schema view - lists the columns(attributes) of the data view and their types.
2 data view - has row on top for the column attributes and rows of data below it.
4. In a table, what do we call the column that serves as the main identifier for a row or data?
primary key
5. What is a foreign key and how is it used ?
When a record in a table is linked to another table by storing its primary key in the second
table, that key is called a foreign key.
6. At a hight level, describe the ActiveRecord pattern:
Active Record facilitates the creation of objects that carry persistent data and data access logic.
Object-Relational Mapping (ORM) is how the Active Record pattern is implemented.
With the ORM programming method, data is converted between incompatible type systems in Object
Oriented programming languages. In effect a "virtual object database" is created which can then be used
from within the programming language. In active record, using this system connects the persistent data
object and access logic to tables (ie. it uses a pattern that wraps models to tables and objects to
rows within the tables which is unique to this ORM) in a relational database management system removing
the need to write SQL statements directly.
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 < ActiveRecord::Base;
has_many :issues
end
class Issue < ActiveRecord::Base;
belongs_to :project
end
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?
model:
class Animal < ActiveRecord::Base;
belongs_to : :zoo
end
foreign_key :zoo_id
schema:
class CreateAnimals < ActiveRecord::Migration
def change
create_table :animals do |t|
t.string :name
t.integer :zoo_id
t.timestamps
end
end
end
What are the methods that are now available to a zoo to call related animals?
Zoo#animals.empty?
Zoo#animals.size?
Zoo#animals
Zoo#animals<<(animal)
Zoo#animals.delete(animal)
Zoo#animals.destroy(animal)
Zoo#animals.find(animal_id)
Zoo#animals.build
Zoo#animals.create
How do I create an animal called "jumpster" in a zoo called "San Diego Zoo"?
animal = Animal.create(name: "dumpster")
zoo = Zoo.create(name: "San Diego Zoo")\
zoo.animals << animal
10. What is mass assignment? What's the non-mass assignment way of setting values?
In Rails when an object is constructed with a parameters hash it is called mass assignment.
eg. Post.new(name: "Skippyjon Jones", description: "Siamese Cat")
attr_accessible and attr_protected prevents mass assignment.
11. What does this code do? Animal.first
Returns the first animal in the animal in the animal 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".
animal = Animal.new(name: "Joe")
animal.save
or
Animal.create(name: "Joe") #This method automatically saves it to the database.
13. How does a M:M association work at the database level?
At the database layer, we use a join table to support the M:M association.
14. What are the two ways to support a M:M association at the ActiveRecord model level? Pros
and cons of the each approach?
1. has_and_belongs_to_many
no join model
implicit join table at db layer
table1_name_table2_name
With this approach can't include additional attributes to include additional information.
2. has_many :through
has a join model
Only disadvantage is that you need to create a join model. However, can easily put additional
attributes (columns) on association.
15. Suppose we have User model and Group model, and we have M:M association all set up.
How do we associate the two?
We use a join table at the database layer which will hold two foreign keys: user_id and group_id
which are the primary keys of the two associated tables.
At the model layer we will create two 1:M associations. One between User and Membership and the
other between Group and Membership. Where Membership is the class of the join model. At the database
layer the join table will be memberships.
class User < ActiveRecord::Base;
has_many :memberships
has_many :groups, through: :memberships
end
class Membership < ActiveRecord::Base;
belongs_to :user
belongs_to :group
end
class Group < ActiveRecord::Base;
has_many :memberships
has_many :users, through: :memberships
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment