Skip to content

Commit a8489e8

Browse files
committed
One to many example
1 parent 3bff801 commit a8489e8

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Tutorial_Docs/Associations.MD

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
## One-to-many
88
* has_many
99
* belongs_to
10+
In ruby console, you can use these methods for has_many
11+
```
12+
subject.pages
13+
subject.pages << pages # append a pages
14+
subject.pages = [page, page, page] # have to append them all if you want to add
15+
subject.pages.delete(page)
16+
subject.pages.destroy(page)
17+
subject.pages.clear
18+
subject.pages.empty?
19+
subject.pages.size
20+
```
21+
1022

1123
## Many to Many
1224
(When join table only has foreign keys)

app/models/subject.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Subject < ApplicationRecord
22

33
# Relations
4-
has_one :page # Doesn't actually have 1, but showing for demonstration purposes
4+
has_many :pages
55

66
# Lamda is read at execution time, which is important for dates.
77
scope :visible, lambda { where(:visible => true) }

0 commit comments

Comments
 (0)