質問編集履歴

5

誤字

2018/07/24 22:52

投稿

messy1019
messy1019

スコア18

test CHANGED
File without changes
test CHANGED
File without changes

4

文章修正

2018/07/24 22:52

投稿

messy1019
messy1019

スコア18

test CHANGED
File without changes
test CHANGED
@@ -9,6 +9,8 @@
9
9
  これにて、micropostsのcontentとusersのnicknameどちらでも検索可能となりました。
10
10
 
11
11
  しかし、「りんご、みかん」というような複数ワードが入力されて検索した場合に対応できていません。
12
+
13
+ and検索で実装したいと考えています。
12
14
 
13
15
 
14
16
 

3

コード修正

2018/07/24 16:38

投稿

messy1019
messy1019

スコア18

test CHANGED
File without changes
test CHANGED
@@ -20,15 +20,33 @@
20
20
 
21
21
 
22
22
 
23
- ```class User < ApplicationRecord
23
+ ```
24
24
 
25
- has_many :microposts, dependent: :destroy
25
+ def show
26
26
 
27
- scope :search_by_keyword, -> (keyword) {
27
+ if logged_in?
28
28
 
29
- where("users.nickname LIKE :keyword", keyword: "%#{sanitize_sql_like(keyword)}%") if keyword.present?
29
+ @micropost = current_user.microposts.build
30
30
 
31
+ if params[:q]
32
+
33
+ relation = Micropost.joins(:user)
34
+
35
+ @feed_items = relation.merge(User.search_by_keyword(params[:q].split(/[\p{blank}\s]+/)))
36
+
37
+ .or(relation.search_by_keyword(params[:q].split(/[\p{blank}\s]+/)))
38
+
39
+ .paginate(page: params[:page])
40
+
41
+ else
42
+
43
+ @feed_items = current_user.feed.paginate(page: params[:page])
44
+
31
- }
45
+ end
46
+
47
+ end
48
+
49
+ end
32
50
 
33
51
  ```
34
52
 

2

コード修正

2018/07/24 15:39

投稿

messy1019
messy1019

スコア18

test CHANGED
File without changes
test CHANGED
@@ -23,24 +23,6 @@
23
23
  ```class User < ApplicationRecord
24
24
 
25
25
  has_many :microposts, dependent: :destroy
26
-
27
- has_many :active_relationships, class_name: "Relationship",
28
-
29
- foreign_key: "follower_id",
30
-
31
- dependent: :destroy
32
-
33
- has_many :passive_relationships, class_name: "Relationship",
34
-
35
- foreign_key: "followed_id",
36
-
37
- dependent: :destroy
38
-
39
- has_many :following, through: :active_relationships, source: :followed
40
-
41
- has_many :followers, through: :passive_relationships, source: :follower
42
-
43
-
44
26
 
45
27
  scope :search_by_keyword, -> (keyword) {
46
28
 

1

誤字修正

2018/07/24 15:37

投稿

messy1019
messy1019

スコア18

test CHANGED
File without changes
test CHANGED
@@ -92,7 +92,7 @@
92
92
 
93
93
 
94
94
 
95
- undefined method `gsub' for ["キーワード①", "キーワード②"]:Array
95
+ undefined method `gsub' for ["りんご", "みかん"]:Array
96
96
 
97
97
 
98
98