質問編集履歴

1

追記

2019/12/03 06:23

投稿

tomtom1
tomtom1

スコア168

test CHANGED
File without changes
test CHANGED
@@ -45,3 +45,87 @@
45
45
  ```
46
46
 
47
47
  お分かりの方、ぜひ助言を宜しくお願いします。
48
+
49
+
50
+
51
+ 追記
52
+
53
+ ```userrb
54
+
55
+ class User < ApplicationRecord
56
+
57
+ has_many :posts, dependent: :delete_all
58
+
59
+ has_many :active_friendships, class_name:"Friendship", foreign_key: "follower_id", dependent: :destroy
60
+
61
+ has_many :passive_friendships, class_name:"Friendship", foreign_key: "followed_id", dependent: :destroy
62
+
63
+ has_many :following, through: :active_friendships, source: :followed
64
+
65
+ has_many :followers, through: :passive_friendships, source: :follower
66
+
67
+
68
+
69
+ def follow(user)
70
+
71
+ active_friendships.create(followed_id: user.id)
72
+
73
+ end
74
+
75
+ def unfollow(user)
76
+
77
+ active_friendships.find_by(followed_id: user.id).destroy
78
+
79
+ end
80
+
81
+ def following?(user)
82
+
83
+ following.include?(user)
84
+
85
+ end
86
+
87
+ def posts
88
+
89
+ return Post.where(user_id: self.id)
90
+
91
+ end
92
+
93
+ def create_notification_follow!(current_user)
94
+
95
+ temp = Notification.where(["visitor_id = ? and visited_id = ? and action = ? ",current_user.id, id, 'follow'])
96
+
97
+ if temp.blank?
98
+
99
+ notification = current_user.active_notifications.new(
100
+
101
+ visited_id: id,
102
+
103
+ action: 'follow'
104
+
105
+ )
106
+
107
+ notification.save if notification.valid?
108
+
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+
115
+
116
+
117
+ ```
118
+
119
+ ```postrb
120
+
121
+ class Post < ApplicationRecord
122
+
123
+ belongs_to :user
124
+
125
+
126
+
127
+ end
128
+
129
+
130
+
131
+ ```