質問編集履歴
2
マイグレーションの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -331,3 +331,33 @@
|
|
331
331
|
end
|
332
332
|
|
333
333
|
```
|
334
|
+
|
335
|
+
```マイグレーション
|
336
|
+
|
337
|
+
class CreateUsers < ActiveRecord::Migration[5.2]
|
338
|
+
|
339
|
+
def change
|
340
|
+
|
341
|
+
create_table :users do |t|
|
342
|
+
|
343
|
+
t.string :name
|
344
|
+
|
345
|
+
t.string :email
|
346
|
+
|
347
|
+
t.string :password_digest
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
t.timestamps
|
352
|
+
|
353
|
+
end
|
354
|
+
|
355
|
+
end
|
356
|
+
|
357
|
+
end
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
コード
|
362
|
+
|
363
|
+
```
|
1
routesとuserモデルの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -180,4 +180,154 @@
|
|
180
180
|
|
181
181
|
|
182
182
|
|
183
|
+
```routes.rb
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
|
188
|
+
|
183
|
-
```
|
189
|
+
```
|
190
|
+
|
191
|
+
Rails.application.routes.draw do
|
192
|
+
|
193
|
+
root to: 'toppages#index'
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
get 'login', to: 'sessions#new'
|
198
|
+
|
199
|
+
post 'login', to: 'sessions#create'
|
200
|
+
|
201
|
+
delete 'logout', to: 'sessions#destroy'
|
202
|
+
|
203
|
+
get 'signup', to: 'users#new'
|
204
|
+
|
205
|
+
resources :users, only: [:index, :show, :new, :create]do
|
206
|
+
|
207
|
+
member do
|
208
|
+
|
209
|
+
get :followings
|
210
|
+
|
211
|
+
get :followers
|
212
|
+
|
213
|
+
get :likes
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
resources :microposts, only: [:create, :destroy]
|
222
|
+
|
223
|
+
resources :relationships, only: [:create, :destroy]
|
224
|
+
|
225
|
+
resources :favorites, only: [:create, :destory]
|
226
|
+
|
227
|
+
delete 'favorites', to: 'favorites#destroy'
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
```
|
232
|
+
|
233
|
+
```user.rb
|
234
|
+
|
235
|
+
class User < ApplicationRecord
|
236
|
+
|
237
|
+
before_save { self.email.downcase! }
|
238
|
+
|
239
|
+
validates :name, presence: true, length: { maximum: 50 }
|
240
|
+
|
241
|
+
validates :email, presence: true, length: { maximum: 255 },
|
242
|
+
|
243
|
+
format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i },
|
244
|
+
|
245
|
+
uniqueness: { case_sensitive: false }
|
246
|
+
|
247
|
+
has_secure_password
|
248
|
+
|
249
|
+
has_many :microposts
|
250
|
+
|
251
|
+
has_many :relationships
|
252
|
+
|
253
|
+
has_many :followings, through: :relationships, source: :follow
|
254
|
+
|
255
|
+
has_many :reverses_of_relationship, class_name: 'Relationship', foreign_key: 'follow_id'
|
256
|
+
|
257
|
+
has_many :followers, through: :reverses_of_relationship, source: :user
|
258
|
+
|
259
|
+
has_many :favorites
|
260
|
+
|
261
|
+
has_many :darling, through: :favorites, source: :micropost
|
262
|
+
|
263
|
+
has_many :reverses_of_ravorite, class_name: 'Favorite', foreign_key: 'micropost_id'
|
264
|
+
|
265
|
+
has_many :favorited, through: :reverses_of_favorite, source: :user
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
def follow(other_user)
|
272
|
+
|
273
|
+
unless self == other_user
|
274
|
+
|
275
|
+
self.relationships.find_or_create_by(follow_id: other_user.id)
|
276
|
+
|
277
|
+
end
|
278
|
+
|
279
|
+
end
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
def unfollow(other_user)
|
284
|
+
|
285
|
+
relationship = self.relationships.find_by(follow_id: other_user.id)
|
286
|
+
|
287
|
+
relationship.destroy if relationship
|
288
|
+
|
289
|
+
end
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
def following?(other_user)
|
294
|
+
|
295
|
+
self.followings.include?(other_user)
|
296
|
+
|
297
|
+
end
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
def feed_microposts
|
302
|
+
|
303
|
+
Micropost.where(user_id: self.following_ids + [self.id])
|
304
|
+
|
305
|
+
end
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
def favorite(micropost)
|
310
|
+
|
311
|
+
self.favorites.find_or_create_by(micropost_id: micropost.id)
|
312
|
+
|
313
|
+
end
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
def unfavorite(micropost)
|
318
|
+
|
319
|
+
favorite = self.favorites.find_by(micropost_id: micropost.id)
|
320
|
+
|
321
|
+
favorite.destroy if favorite
|
322
|
+
|
323
|
+
end
|
324
|
+
|
325
|
+
def favorited?(micropost)
|
326
|
+
|
327
|
+
self.darling.include?(micropost)
|
328
|
+
|
329
|
+
end
|
330
|
+
|
331
|
+
end
|
332
|
+
|
333
|
+
```
|