Learning Ruby

Reference, Mnemonic & Ramblings

Quiz for Rails Beginners 1

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.

  1. Why do they call it a relational database?

  2. What is SQL?

  3. There are two predominant views into a relational database. What are they, and how are they different?

  4. 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.

  5. What is a foreign key, and how is it used?

  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.

  7. If there's an ActiveRecord model called "CrazyMonkey", what should the table name be?

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

  9. Given this code
    1
    2
    3
    
    class Zoo < ActiveRecord::Base
      has_many :animals
    end
    

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

  11. What does this code do? Animal.first

  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?

  13. How does a M:M association work at the database level?

  14. What are the two ways to support a M:M association at the ActiveRecord model level? Pros and cons of each approach?

  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?

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

A3:
  • 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)
Back

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:

Project Model
1
2
3
class Project < ActiveRecord::Base
  has_many :issues
end
Issue Model
1
2
3
class Issue < ActiveRecord::Base
  belongs_to :project
end

Foreign Key: project_id

Back

A9:

The Animal Model and Schemas

Animal Model
1
2
3
class Animal < ActiveRecord::Base
  belongs_to :zoo
end

The Zoo table schema:
  • id (integer, primary_key, unique)
  • name (string)
The Animal table schema:
  • id (integer, primary_key, unique)
  • name/species (string)
  • zoo_id (integer, foreign_key)

Methods available to a ‘zoo’ object to call related to animals:
  • 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 = {})

To create an animal called “jumpster” in a zoo called “San Diego Zoo”:
Rails Console
1
2
zoo=Zoo.find_by(name: "San Diego Zoo")
zoo.animals << Animal.create(name: "jumpster")

Back

A10:
  • 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
    
Back

A11: It will fetch the first defined object (row) in the animals table in the database
Back

A12:
  • Animal.create(name: 'Joe')
  • Animal.create!(name: 'Joe')
Back

A13: It works through a join table which has attributes to store foreign keys (IDs) of the two tables it joins.
Back

A14:
  • 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)
Back

A15:
Rails Console
1
2
3
user  = User.find(1)
group = Group.find(1)
user.group << group
Back

comments powered by Disqus