Skip to content

Instantly share code, notes, and snippets.

@rebrinktexas
Created January 10, 2014 16:11
Show Gist options
  • Save rebrinktexas/8357173 to your computer and use it in GitHub Desktop.
Save rebrinktexas/8357173 to your computer and use it in GitHub Desktop.
Week 1 Quiz Answers
Rails Week 1 Quiz
Why do they call it a relational database?
Ted Codd defined the relational databases in a paper in 1970. The major feature of the model was rows of data, called “tuples”, and a tables of rows, called “relations”. There are base relations, with a physical instantiation of the tuples, and view relations, with a more ephemeral instantiation of tuples.
The Relational Model was a significant change from the hierarchical and network database designs previously popular. The relational model emphasizes the durability of the relationship between tables even though the physical implementation might vary with index and file storage methods. Interestingly, the most current trend in No-SQL databases releases the relational constraints and returns to less flexible and higher performing database structures.
What is SQL?
SQL is the language used to manipulate data in a relational database. Structured Query Language (SQL) was designed to manipulate the relations with mathematical precision and is based on set theory. All RDBMS vendors support SQL, and often lower level APIs, for query and update. There is an ANSI standard for SQL that is typically a subset of each RDBMS implementation.
There are two predominant views into a relational database. What are they, and how are they different?
There is a view of the schema of the relations in a database. This view shows the logical description of the data to be stored. As an example – the table might have a column named “PublicBuilding” that is structured as text.
There is and a view of the actual data that is stored in the relation. As an example, PubblicBuilding might contain these data items in different rows - “Zoo”, “Park”, “Train Station”.
There is also a third view that is less commonly seen by application developers – the physical view. The physical view shows the actual file structure that the logical view is implemented with.
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.
SQL will select data from a table by comparing the data against a single value or set of values. A relation may have a column with unique values. When the physical storage of the table is organized by an index on that field – it is a primary key.
What is a foreign key, and how is it used?
A foreign key is an indexed column, that provides a logical connection to a primary key in another table. It used to create better performance with an SQL statement.
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.
The implementation of the pattern provides enhanced capability to manage data in a relational database from a programming language or development environment. Each row of a table is wrapped as an object, and accessor methods are made available to the developer. The ActiveRecord implementation for Ruby/Rails adds capabilities for inheritance and associations.
If there's an ActiveRecord model called "CrazyMonkey", what should the table name be?
crazy_monkeys
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 Issues<ActiveRecord::Base
belongs_to :project
end
Foreign key:
issues.project_id
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
end
ActiveRecord::Schema.define(version: 20140108003441) do
create_table “animals”, do |t|
t.string “name”
t.date_time “created_at”
t.date_time “updated_at”
t.integer “zoo_id”
end
end
What are the methods that are now available to a zoo to call related to animals?
animals(force_reload = false)
animals<<(object, ...)
animals.delete(object, ...)
animals.destroy(object, ...)
animals=objects
animal_ids
animal_ids=ids
animals.clear
animals.empty?
animals.size
animals.find(...)
animals.where(...)
animals.exists?(...)
animals.build(attributes = {}, ...)
animals.create(attributes = {})
How do I create an animal called "jumpster" in a zoo called "San Diego Zoo"?
animal=Animal.create(zoo_name: “San Diego”, animal_name: “Jumpster”)
What is mass assignment? What's the non-mass assignment way of setting values?
Mass assignment is setting multiple attributes in a single statement.
Non-mass assignment is setting each attribute in a separate statement.
What does this code do? Animal.first
Returns the first tuple in the table “animals”
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=Animal.new
animal.name=”Joe”
animal.save
How does a M:M association work at the database level?
One builds a join table that has columns for the primary key of each table. The association builds a row for each new primary key instance.
What are the two ways to support a M:M association at the ActiveRecord model level? Pros and cons of each approach?
Rails offers two different ways to declare a many-to-many relationship between models. The simpler way is to use has_and_belongs_to_many, which allows you to make the association directly.
The second way to declare a many-to-many relationship is to use has_many :through. This makes the association indirectly, through a join model.
The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the relationship model as an independent entity. If you don't need to do anything with the relationship model, it may be simpler to set up a has_and_belongs_to_many relationship (though you'll need to remember to create the joining table in the database).
You should use has_many :through if you need validations, callbacks, or extra attributes on the join model.
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 < ActiveRecord::Base
has_and_belongs_to_many :groups
end
Class Group < ActiveRecord::Base
has_and_belongs_to_many :users
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment