Skip to content

Instantly share code, notes, and snippets.

@jamesyang124
Last active December 26, 2015 13:18
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 jamesyang124/7156920 to your computer and use it in GitHub Desktop.
Save jamesyang124/7156920 to your computer and use it in GitHub Desktop.

Rails Week 1 Quiz

  1. Why do they call it a relational database?
  • The database composite by tables and each table relate to other tables via the foreign keys. The tables composite by attribute column and data row to express a collection of data. By the way, primary key is for table itself quering.
  1. What is SQL?
  • Structural query language. A standard syntax to qurey and operate the database.
  1. There are two predominant views into a relational database. What are they, and how are they different?
  • Schema is one of the view to describe the data sctructre, data types, another view is the rows in each table to describe a value collection[row] of attributes[column], which is data view.
  • Data view describes data with its value and attribute name, Schema view desrcibes attributes' types and structure.
  1. 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.
  • Primary key.
  1. What is a foreign key, and how is it used?
  • It is an index to relate with anthoer table's row. It usually links the value with another table's primary key's value.
  1. 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.
  • It's a way to map the relational database with object-oriented concept. It treats each model object as an row in a table. Model object's methods or attributes may operate a state or as SQL in database.
  1. If there's an ActiveRecord model called "CrazyMonkey", what should the table name be?
  • carzy_monkeys.
  1. If I'm building a 1:M association between Project and Issue, what will the model associations and foreign key be?

      #Issue.rb 
      belongs_to :project, foreign_key: 'project'
    
      #Project.rb
      has_many :issues
    
  2. Given this code

class Zoo 
      has_many :animals
end
- What do you expect the other model to be and what does database schema look like?
  + Animal model. animals table, zoos table. In animals table with fk key: zoo_id   
- What are the methods that are now available to a zoo to call related to animals?

  ```
  @zoo.animals << @animal
  @zoo.animals.create(animal: @animal)
  @zoo.animals.new().save  
  ```
- How do I create an animal called "jumpster" in a zoo called "San Diego Zoo"?

  ```
  animal = Animal.create(name: "jumpster")
  zoo = Zoo.find(:first, conditions: ["name='San Diego Zoo'"])
  zoo = Zoo.where(name: 'San Diego Zoo')
  zoo.animals << animal
  ```
  1. What is mass assignment? What's the non-mass assignment way of setting values?
    • Mass assignment is using key/value pair hash as arguments to the Model::create() or Model::new() methods.
    • Non-mass assignment is using Model's setters to set values.
  2. What does this code do? Animal.first
    • Get first existed row data in animals table. And AR returns a model object which represents that data row.
  3. 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?
    • Use Model.create() method, which autosave the data back to database.
  4. How does a M:M association work at the database level?
    • It implements two 1:M relations for both tables. A join table will be included and associated two tables with two tables' foreign keys.
  5. What are the two ways to support a M:M association at the ActiveRecord model level? Pros and cons of each approach?
    • has_many through or has_and_belongs_to_many
    • has_many through
      • pros: Provides flexibility, especially the join table should be treated as an entity.
      • cons: More configuration, schema, and migration setup. Requires a join model in rails for the join table.
    • has_and_bleongs_to_many
      • pros: Simplicity. The join table which should only has two foreign keys and primary key for it. It reuqires a join table in database but does not require a join model in rails.
      • cons: Restrict the table name must create by its convention(compare two tables with String::< for the join table name), the join table should not with other attributes.
  6. 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? - Create a join table with foreign key for both tables. If we associated both tables with has_many through, then the creation of join model is required also.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment