Here's a quiz for people who are just getting started learning web-application development. Most questions are specific to Rails, others are generic. Each question is a link pointing to my corresponding answer as i understand the concepts the today.
There are two predominant views into a relational database. What are they, and how are they different?
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.
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.
If there's an ActiveRecord model called "CrazyMonkey", what should the table name be?
If I'm building a 1:M association between Project and Issue, what will the model associations and foreign key be?
- Given this code
1 2 3
class Zoo < ActiveRecord::Base has_many :animals end
What is mass assignment? What's the non-mass assignment way of setting values?
What does this code do?
Animal.first
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?
What are the two ways to support a M:M association at the ActiveRecord model level? Pros and cons of each approach?
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?
MY ANSWERS:
A1: The way data is represented in tuples (ordered list or array) of attribute-values that are grouped in a relation (tuple with attribute/title/header) makes this database a relational database. Source
Back
A2: Structured Query Language is a language to create, read, update, and destroy information in a Relational Database.
Back
- The schema view shows what columns are in a table and what type of data (string/integer/boolean/…) each columns can store
- The data view shows the actual table with the data that is currently stored in it (like a spreadsheet view)
A4: Primary Key
Back
A5: Foreign key is an index stored in one table that refers to the row index (primary key) of another table.
Back
A6: Active Record pattern is an approach to accessing data in a database. Typical operations it helps perform on the database are inserting, updating and deleting data. Reference
Back
A7: crazy_monkeys
Back
A8:
1
2
3
class Project < ActiveRecord::Base
has_many :issues
end
1
2
3
class Issue < ActiveRecord::Base
belongs_to :project
end
Foreign Key: project_id
The Animal Model and Schemas
1
2
3
class Animal < ActiveRecord::Base
belongs_to :zoo
end
id
(integer, primary_key, unique)name
(string)
id
(integer, primary_key, unique)name
/species
(string)zoo_id
(integer, foreign_key)
animals
animals << animal_object
animals.delete(object, ...)
animals.destroy(object, ...)
animals=animal_objects
animal_ids
animal_ids=ids
animals.clear
animals.empty?
animals.size
animals.find(...)
animals.where(...)
animals.exists?(...)
animals.build(attributes = {}, ...)
animals.create(attributes = {})
animals.create!(attributes = {})
1 2 |
|
- Mass assignment -
Animal.create(name: 'tiger', zoo_id: '3')
- Non-mass assignment -
1 2 3
a = Animal.create() a.name = 'tiger' a.zoo_id = 3
A11: It will fetch the first defined object (row) in the animals
table in the database
Back
-
Animal.create(name: 'Joe')
-
Animal.create!(name: 'Joe')
A13: It works through a join table which has attributes to store foreign keys (IDs) of the two tables it joins.
Back
-
has_many, :through
(hmt)- Pro: Join medium is a model (can have additional attributes)
-
has_and_belongs_to_many
(habtm)- Con: Only Join-Table, no Join-Model so cannot have custom attributes (as against the 'Pro' for 'hmt' type association)
1 2 3 |
|