Quiz: Week 1

Quiz: Week 1

1. Why do they call it a relational database?

The database is put into a real world of abstract objects, such as students and courses. These objects exist in different tables, there is a relationship between each of the tables.

2. What is SQL?

SQL(Structured Query Language) is a standard language for accessing and manipulating databases.

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

*Schema view: *

show all of the data in table, and the type of data. It provide a global view to tables, similar to the book index.

*Data view: *

show the data values. Usually the user can directly modify the data in this view. (SELECT, ADD, UPDATE, DELETE)

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.

‘Primary Key'

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

Table’s foreign key point to another table’s primary key. Than the two tables have the relationship. Foreign key can be used in build relationships, or called to organize data.

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.

Without ActiveRecord, programer can only use SQL to organize data. It’s very painful and inefficient.

With the ActiveRecord, we can organize data like nature language.

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

“crazy_monkeys"

If you like me with poor English, use the “”.tableize method in rails console. I just made a mistake, create “Categorys” wrote to “Categories”. - -!

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

  1. class Zoo < ActiveRecord::Base

  2.     has_many :animals

   3. end
  • What do you expect the other model to be and what does database schema look like?
  • What are the methods that are now available to a zoo to call related to animals?
  • How do I create an animal called "jumpster" in a zoo called "San Diego Zoo”?

    class Animal < ActiveRecord::Base
      belongs_to     :zoo
    end
    
    
    class CreateAnimals < ActiveRecord::Migration
      def change
        create_table     :animals do |t|
          t.string     :animalname
          t.integer  :zoo_id
    
    
    
      t.timestamps
    end
    
    end end class CreateZoos < ActiveRecord::Migration def change create_table :zoos do |t| t.string :zooname t.timestamps end end end

rails console:

zoo = Zoo.create(zooname: ”San Diego Zoo”)

Animal.create(animalname: “jumpster”, zoo_id: zoo.id)

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

Passing in the key and values in hash. Than passing hash as arguments. Is that right?

a) mass assignment:

    class FootballPlayer 
      attr_accessor :first_name, :last_name 
      def initialize(first_name = nil, last_name = nil) 
        @first_name = first_name 
        @last_name = last_name 
      end 
    end

b)non-mass assignment:

    def initialize(options = { }) 
        @first_name = options[:first_name]
      @last_name = options[:last_name] 
    end

11. What does this code do? Animal.first

SELECT “animals".* FROM “animals" ORDER BY “animals"."id" ASC LIMIT 1

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?

a) Create migration file and model file as question 9.

b) rails console:

animal = Animal.new(animalname: “Joe”)
animal.save

or

animal = Animal.create(animalname: “Joe”)

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

M:M work at the database level by join table, some times join table has its own data, some times join table only have foreign key data. Its depend on the apps needs.

14. 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 hasandbelongstomany, which allows you to make the association directly:

class Assembly < ActiveRecord::Base
  has_and_belongs_to_many :parts
end

class Part < ActiveRecord::Base
  has_and_belongs_to_many :assemblies
end

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:

class Assembly < ActiveRecord::Base
  has_many :manifests
  has_many :parts, through: :manifests
end

class Manifest < ActiveRecord::Base
  belongs_to :assembly
  belongs_to :part
end

class Part < ActiveRecord::Base
  has_many :manifests
  has_many :assemblies, through: :manifests
end

If I use destroy method destroy association table data in first way, the join table’s data will be destroy.
The second way won’t destroy join table’s data, is that right?

The simplest rule of thumb is that you should set up a hasmany :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 hasandbelongsto_many relationship.

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?

Please see the code on GitHub:
https://github.com/vivijie/tealeaf_pre_course

Neil Wang

产品经理。喜欢摄影,滑板,电影。微信公众号:NeilOnly