ORM : Object Relational Mapper
create, read, update, delete
class User < ActiveRecord::Base
end
------------------------------------
| Users Table |
+----+---------+-------------------+
| ID | Name | Movie |
|----+---------+-------------------|
| 1 | Richard | zoolander |
|----+---------+-------------------|
| 2 | Ruby | princess bride |
|----+---------+-------------------|
| 3 | Chris | sandlot |
+----+---------+-------------------|
answer:
princess_bride = User.find_by(id:2).movie
-
A. Foreign
-
B. Primary (my answer)
-
C. Public
-
D. Private
answer: MAX
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:
-
"John" wrote a post titled "I love dogs" with the content "woof", "richard" commented "that was great", susan commented "cats are better"
-
"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
my answer:
+----+----------------+-------------+------------------+
| posts |
+----+----------------+-------------+------------------+
| id | title | author_name | body |
+----+----------------+-------------+------------------+
| 1 | I love dogs | John | woof |
| 2 | cars are great | Sara | I think they are |
+----+----------------+-------------+------------------+
+----+---------+-----------------+---------+
| comments |
+----+---------+-----------------+---------+
| 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".
answer:
john_post = Post.find_by(author_name: "John")
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