5. Database Types
Rails was designed with a relational database bias
Though it can work with other tools, we will stick with
this primary focus
6. Database Types
Rails was designed with a relational database bias
Though it can work with other tools, we will stick with
this primary focus
ActiveRecord can work with the SQL these databases
use to make queries
7. Database Types
Rails was designed with a relational database bias
Though it can work with other tools, we will stick with
this primary focus
ActiveRecord can work with the SQL these databases
use to make queries
One of the ways it does that is to manage the
relationships of data in separate tables
13. Relationship Types
There are only three types of relationships in SQL
One to one
One to many
Many to many
This doesn’t really exist, but it can be simulated
14. One to One
users
id first_name last_name
1 James Gray
2 Dana Gray
photos
id user_id path
1 1 /images/james.png
15. One to One
users
id first_name last_name
1 James Gray
2 Dana Gray
photos
id user_id path
1 1 /images/james.png
16. One to Many
articles
id title body comments
1 The CRUD … id article_id body
2 Associations … 1 1 First!
2 1 Tard.
3 1 Am not!
4 2 Viagra
5 2 Spam!
17. One to Many
articles
id title body comments
1 The CRUD … id article_id body
2 Associations … 1 1 First!
2 1 Tard.
3 1 Am not!
4 2 Viagra
5 2 Spam!
18. One to Many
articles
id title body comments
1 The CRUD … id article_id body
2 Associations … 1 1 First!
2 1 Tard.
3 1 Am not!
4 2 Viagra
5 2 Spam!
19. photos
Many to Many id path
1 /images/mac.jpg
taggings 2 /images/pc.bmp
id photo_id tag_id
1 1 1 tags
id name
2 1 2
1 computer
3 1 3
2 sexy
4 2 4
3 lickable
4 junk
20. photos
Many to Many id path
1 /images/mac.jpg
taggings 2 /images/pc.bmp
id photo_id tag_id
1 1 1 tags
id name
2 1 2
1 computer
3 1 3
2 sexy
4 2 4
3 lickable
4 junk
21. photos
Many to Many id path
1 /images/mac.jpg
taggings 2 /images/pc.bmp
id photo_id tag_id
1 1 1 tags
id name
2 1 2
1 computer
3 1 3
2 sexy
4 2 4
3 lickable
4 junk
22. Many to Many (Two Tables)
followings users
id follower_id followed_id id first_name last_name
1 1 4 1 James Gray
2 2 4 2 Dana Gray
3 3 1 3 Super Man
4 4 1 4 Bat Man
5 5 1 5 Spider Man
23. Many to Many (Two Tables)
followings users
id follower_id followed_id id first_name last_name
1 1 4 1 James Gray
2 2 4 2 Dana Gray
3 3 1 3 Super Man
4 4 1 4 Bat Man
5 5 1 5 Spider Man
24. Many to Many (Two Tables)
followings users
id follower_id followed_id id first_name last_name
1 1 4 1 James Gray
2 2 4 2 Dana Gray
3 3 1 3 Super Man
4 4 1 4 Bat Man
5 5 1 5 Spider Man
25. Conventions at Work
articles
id title body
1 Unique …
comments
id article_id body
1 1 First!
26. Conventions at Work
Rails favors conventions articles
id title body
1 Unique …
comments
id article_id body
1 1 First!
27. Conventions at Work
Rails favors conventions articles
With associations: id title body
1 Unique …
comments
id article_id body
1 1 First!
28. Conventions at Work
Rails favors conventions articles
With associations: id title body
Auto-incremented ID 1 Unique …
fields
comments
id article_id body
1 1 First!
29. Conventions at Work
Rails favors conventions articles
With associations: id title body
Auto-incremented ID 1 Unique …
fields
Table name is plural
comments
for the collection
id article_id body
1 1 First!
30. Conventions at Work
Rails favors conventions articles
With associations: id title body
Auto-incremented ID 1 Unique …
fields
Table name is plural
comments
for the collection
id article_id body
Foreign key is a
singular item plus _id 1 1 First!
34. Rails Macros
Use belongs_to() on the ID side of a one to one or one
to many
Use has_one() on the other side of a one to one
35. Rails Macros
Use belongs_to() on the ID side of a one to one or one
to many
Use has_one() on the other side of a one to one
Use has_many() on the other side of a one to many
36. Rails Macros
Use belongs_to() on the ID side of a one to one or one
to many
Use has_one() on the other side of a one to one
Use has_many() on the other side of a one to many
Use two belongs_to() calls for the join model of a
many to many
37. Rails Macros
Use belongs_to() on the ID side of a one to one or one
to many
Use has_one() on the other side of a one to one
Use has_many() on the other side of a one to many
Use two belongs_to() calls for the join model of a
many to many
Use has_many() and has_many(…, :through => …)
for the data models of a many to many
38. One to Many
articles
id title body comments
1 The CRUD … id article_id body
2 Associations … 1 1 First!
2 1 Tard.
3 1 Am not!
4 2 Viagra
5 2 Spam!
40. class Article < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :article
end
One to Many
An article has_many() comments
60. Using Associations Correctly
You should very rarely need to refer to an ID
Let Rails worry about that for you
It’s safer to scope as many queries as possible through
the association
61. Using Associations Correctly
You should very rarely need to refer to an ID
Let Rails worry about that for you
It’s safer to scope as many queries as possible through
the association
There’s less chance to affect the wrong data
62. Using Associations Correctly
You should very rarely need to refer to an ID
Let Rails worry about that for you
It’s safer to scope as many queries as possible through
the association
There’s less chance to affect the wrong data
It adds security against users trying to spoof ID’s
63. One to One
users
id first_name last_name
1 James Gray
2 Dana Gray
photos
id user_id path
1 1 /images/james.png
71. class Photo < ActiveRecord::Base
has_many :taggings
has_many :tags, :through => :taggings
end
class Tagging < ActiveRecord::Base
belongs_to :photo
belongs_to :tag
end
class Tag < ActiveRecord::Base
has_many :taggings
has_many :photos, :through => :taggings
end
Many to Many
Photos have many tags
72. class Photo < ActiveRecord::Base
has_many :taggings
has_many :tags, :through => :taggings
end
class Tagging < ActiveRecord::Base
belongs_to :photo
belongs_to :tag
end
class Tag < ActiveRecord::Base
has_many :taggings
has_many :photos, :through => :taggings
end
Many to Many
Photos have many tags
78. Many to Many (Two Tables)
followings users
id follower_id followed_id id first_name last_name
1 1 4 1 James Gray
2 2 4 2 Dana Gray
3 3 1 3 Super Man
4 4 1 4 Bat Man
5 5 1 5 Spider Man
79. Many to Many (Two Tables)
Users have many followers
80. class Following < ActiveRecord::Base
belongs_to :follower, :class_name => "User",
:foreign_key => :follower_id
belongs_to :followed, :class_name => "User",
:foreign_key => :followed_id
end
class User < ActiveRecord::Base
has_many :other_followings, :class_name => "Following",
:foreign_key => :followed_id
has_many :followers, :through => :other_followings
has_many :self_followings, :class_name => "Following",
:foreign_key => :follower_id
has_many :followed_by, :through => :self_followings,
:source => :followed
end
Many to Many (Two Tables)
Users have many followers
81. class Following < ActiveRecord::Base
belongs_to :follower, :class_name => "User",
:foreign_key => :follower_id
belongs_to :followed, :class_name => "User",
:foreign_key => :followed_id
end
class User < ActiveRecord::Base
has_many :other_followings, :class_name => "Following",
:foreign_key => :followed_id
has_many :followers, :through => :other_followings
has_many :self_followings, :class_name => "Following",
:foreign_key => :follower_id
has_many :followed_by, :through => :self_followings,
:source => :followed
end
Many to Many (Two Tables)
Users have many followers
82. class Following < ActiveRecord::Base
belongs_to :follower, :class_name => "User",
:foreign_key => :follower_id
belongs_to :followed, :class_name => "User",
:foreign_key => :followed_id
end
class User < ActiveRecord::Base
has_many :other_followings, :class_name => "Following",
:foreign_key => :followed_id
has_many :followers, :through => :other_followings
has_many :self_followings, :class_name => "Following",
:foreign_key => :follower_id
has_many :followed_by, :through => :self_followings,
:source => :followed
end
Many to Many (Two Tables)
Users have many followers
83. class Following < ActiveRecord::Base
belongs_to :follower, :class_name => "User",
:foreign_key => :follower_id
belongs_to :followed, :class_name => "User",
:foreign_key => :followed_id
end
class User < ActiveRecord::Base
has_many :other_followings, :class_name => "Following",
:foreign_key => :followed_id
has_many :followers, :through => :other_followings
has_many :self_followings, :class_name => "Following",
:foreign_key => :follower_id
has_many :followed_by, :through => :self_followings,
:source => :followed
end
Many to Many (Two Tables)
Users have many followers
92. Finder Options
and Dependance
Set :conditions, :order, etc. on associations
Example: :order => “created_at DESC”
Specify what happens to associations with destroy()
93. Finder Options
and Dependance
Set :conditions, :order, etc. on associations
Example: :order => “created_at DESC”
Specify what happens to associations with destroy()
:dependent => :destroy forwards destroy()
94. Finder Options
and Dependance
Set :conditions, :order, etc. on associations
Example: :order => “created_at DESC”
Specify what happens to associations with destroy()
:dependent => :destroy forwards destroy()
:dependent => :nullify breaks the link
98. Polymorphic Associations
Rails has a special type of “Polymorphic Association”
Models are linked with a type and an ID
This makes it possible to link one model to multiple
different kinds of other models
99. Polymorphic Associations
Rails has a special type of “Polymorphic Association”
Models are linked with a type and an ID
This makes it possible to link one model to multiple
different kinds of other models
For example, you might use a Comment model to
allows users to comment on multiple things
(products and reviews for example)