Skip to content

Instantly share code, notes, and snippets.

@schneems
Created June 21, 2012 22:14
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 schneems/2968916 to your computer and use it in GitHub Desktop.
Save schneems/2968916 to your computer and use it in GitHub Desktop.
Ruby & Databases Recap Quiz for Week 2 [Solutions]
## Week 3 Quiz
## 1) What does ORM stand for?
Object Relationship Mapper
## 2) What are the four functions of persistent storage? (hint: CRUD)
Create Read Update Delete
## 3) The result is 'princess bride' what was the query? You can use Ruby (ActiveRecord) or SQL
class User < ActiveRecord::Base
end
------------------------------------
| Users Table |
+----+---------+-------------------+
| ID | Name | Movie |
|----+---------+-------------------|
| 1 | Richard | zoolander |
|----+---------+-------------------|
| 2 | Ruby | princess bride |
|----+---------+-------------------|
| 3 | Chris | sandlot |
+----+---------+-------------------+
SELECT movie FROM users WHERE name = 'Ruby';
or
User.where(:name => 'Ruby').first.name
## 4) What type of key does the ID column represent in the Users table above
A) Foreign
B) Primary
C) Public
D) Private
The answer is A) Primary
## 5) MIN is a database function, list one more
COUNT
MAX
AVG
and many more
## 6) In Rails we could build a blog. On each blog post we might want comments. We could say that a Post has_many :comments and that a Comment belongs to :post.
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
A post has properties :title, :author_name, and :body, a comment has properties :name & :body. Sketch out a posts table and a comments table including the primary and foreign keys. Use this data:
1) "John" wrote a post titled "I love dogs" with the content "woof", "richard" commented "that was great", susan commented "cats are better"
2) "Sara" wrote a post titled "cars are great" with the content " I think they are", "james" commented "they are"
Sketch this out so it looks like the users table above
(formatted using http://www.sensefulsolutions.com/2010/10/format-text-as-table.html )
Posts Table
+----+----------------+-------------+-------------------+
| ID | title | author_name | body |
+----+----------------+-------------+-------------------+
| 1 | I love dogs | john | woof |
| 2 | cars are great | sara | I think they are |
+----+----------------+-------------+-------------------+
Comments Table
+----+---------+-----------------+---------+
| ID | name | body | post_id |
+----+---------+-----------------+---------+
| 1 | richard | that was great | 1 |
| 2 | susan | cats are better | 1 |
| 3 | james | they are | 2 |
+----+---------+-----------------+---------+
## 7) Using only Ruby (ActiveRecord) and using the data from the above example, how would we find all the posts that were written by "john".
Post.where(:author_name => 'john').first
## 8) What is the output of the following
puts "hello".class
# => String
puts {:bird => 'tweet'}.class
# => Hash
class Adult
end
class Child < Adult
end
little_richie = Child.new
puts little_richie.class
# => Child
Hints:
select, where, count, from, *, =
@MrBri
Copy link

MrBri commented Nov 5, 2012

I believe for question number 3 it should be in Ruby:
User.where(:name => 'Ruby').first.movie
or simplified:
User.where(name:'Ruby').movie

@zallanx
Copy link

zallanx commented Dec 3, 2012

Agreed with MrBri.. "name" would just return "Ruby" again.

@nanoxd
Copy link

nanoxd commented Apr 18, 2013

Number 4 should be changed to:

B) Primary

@barnett
Copy link

barnett commented May 16, 2013

Ya, I agree with both the fixes described above.

@sngliwei
Copy link

sngliwei commented Oct 7, 2013

Think the answer for #7 should be:

Post.where(:author_name => 'john')

— without the "first" since we're looking for all entries?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment