teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

誤字

2018/07/24 22:52

投稿

messy1019
messy1019

スコア18

title CHANGED
File without changes
body CHANGED
File without changes

4

文章修正

2018/07/24 22:52

投稿

messy1019
messy1019

スコア18

title CHANGED
File without changes
body CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  これにて、micropostsのcontentとusersのnicknameどちらでも検索可能となりました。
6
6
  しかし、「りんご、みかん」というような複数ワードが入力されて検索した場合に対応できていません。
7
+ and検索で実装したいと考えています。
7
8
 
8
9
  現在のmodelsとcontorollerのコードはそれぞれ以下となります。
9
10
 

3

コード修正

2018/07/24 16:38

投稿

messy1019
messy1019

スコア18

title CHANGED
File without changes
body CHANGED
@@ -9,11 +9,20 @@
9
9
 
10
10
  controller
11
11
 
12
+ ```
13
+ def show
14
+ if logged_in?
12
- ```class User < ApplicationRecord
15
+ @micropost = current_user.microposts.build
16
+ if params[:q]
13
- has_many :microposts, dependent: :destroy
17
+ relation = Micropost.joins(:user)
18
+ @feed_items = relation.merge(User.search_by_keyword(params[:q].split(/[\p{blank}\s]+/)))
14
- scope :search_by_keyword, -> (keyword) {
19
+ .or(relation.search_by_keyword(params[:q].split(/[\p{blank}\s]+/)))
20
+ .paginate(page: params[:page])
21
+ else
15
- where("users.nickname LIKE :keyword", keyword: "%#{sanitize_sql_like(keyword)}%") if keyword.present?
22
+ @feed_items = current_user.feed.paginate(page: params[:page])
16
- }
23
+ end
24
+ end
25
+ end
17
26
  ```
18
27
  models/micropost.rb
19
28
 

2

コード修正

2018/07/24 15:39

投稿

messy1019
messy1019

スコア18

title CHANGED
File without changes
body CHANGED
@@ -11,15 +11,6 @@
11
11
 
12
12
  ```class User < ApplicationRecord
13
13
  has_many :microposts, dependent: :destroy
14
- has_many :active_relationships, class_name: "Relationship",
15
- foreign_key: "follower_id",
16
- dependent: :destroy
17
- has_many :passive_relationships, class_name: "Relationship",
18
- foreign_key: "followed_id",
19
- dependent: :destroy
20
- has_many :following, through: :active_relationships, source: :followed
21
- has_many :followers, through: :passive_relationships, source: :follower
22
-
23
14
  scope :search_by_keyword, -> (keyword) {
24
15
  where("users.nickname LIKE :keyword", keyword: "%#{sanitize_sql_like(keyword)}%") if keyword.present?
25
16
  }

1

誤字修正

2018/07/24 15:37

投稿

messy1019
messy1019

スコア18

title CHANGED
File without changes
body CHANGED
@@ -45,7 +45,7 @@
45
45
 
46
46
  この状態ですと、以下のエラーが発生します。
47
47
 
48
- undefined method `gsub' for ["キーワード①", "キーワード②"]:Array
48
+ undefined method `gsub' for ["りんご", "みかん"]:Array
49
49
 
50
50
  modelsを修正しないといけないと思うのですが、どのように修正したら良いか分かりません。
51
51
  もし、お分かりになる方がいらっしゃいましたら教えて頂ければ幸いです。