質問編集履歴
3
コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -19,3 +19,55 @@
|
|
19
19
|
しかしこれだとエラーになるようで、
|
20
20
|
|
21
21
|
どのようにすれば取り出せますか?
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
```
|
26
|
+
|
27
|
+
class User
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
follow 関連
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
has_many :relationships
|
36
|
+
|
37
|
+
has_many :followings, through: :relationships, source: :follow
|
38
|
+
|
39
|
+
has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id'
|
40
|
+
|
41
|
+
has_many :followers, through: :reverse_of_relationships, source: :user
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
def follow(other_user)
|
46
|
+
|
47
|
+
unless self == other_user
|
48
|
+
|
49
|
+
self.relationships.find_or_create_by(follow_id: other_user.id)
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
def unfollow(other_user)
|
58
|
+
|
59
|
+
relationship = self.relationships.find_by(follow_id: other_user.id)
|
60
|
+
|
61
|
+
relationship.destroy if relationship
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
def following?(other_user)
|
68
|
+
|
69
|
+
self.followings.include?(other_user)
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
```
|
2
コード修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
-
@folow_users = @user.followings
|
13
|
+
@follow_users = @user.followings
|
14
14
|
|
15
15
|
```
|
16
16
|
|
1
コード修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
-
@folow_users = @user.
|
13
|
+
@folow_users = @user.followings
|
14
14
|
|
15
15
|
```
|
16
16
|
|