質問編集履歴
4
ついき
test
CHANGED
File without changes
|
test
CHANGED
@@ -66,7 +66,9 @@
|
|
66
66
|
|
67
67
|
skills = user.user_skills
|
68
68
|
|
69
|
+
|
70
|
+
|
69
|
-
user_skill_ids = []
|
71
|
+
user_skill_ids = [] #一人のuserが所持しているskillsのidの配列
|
70
72
|
|
71
73
|
|
72
74
|
|
3
ついき
test
CHANGED
File without changes
|
test
CHANGED
@@ -151,3 +151,27 @@
|
|
151
151
|
|
152
152
|
|
153
153
|
```
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
``` ruby
|
158
|
+
|
159
|
+
# リクエストはこんなかんじで来ています
|
160
|
+
|
161
|
+
# parametersのnameというのは、User検索とHost検索という2種類ユーザーに対応するためです。Host検索にリクエストされた場合にparamsを受け取って使う予定でいます。
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
Started GET "/api/users?page=1&name=&address=&lowerYear=0&skillsId[]=1&skillsId[]=9" for 172.21.0.1 at 2021-12-22 02:29:28 +0000
|
170
|
+
|
171
|
+
Processing by Api::UsersController#index as HTML
|
172
|
+
|
173
|
+
Parameters: {"page"=>"1", "name"=>"", "address"=>"", "lowerYear"=>"0", "skillsId"=>["1", "9"]}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
```
|
2
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -133,3 +133,21 @@
|
|
133
133
|
end
|
134
134
|
|
135
135
|
```
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
|
141
|
+
#サンプルデータ
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
<User id: 30, provider: "email", uid: "1@1.com", allow_password_change: false, name: "1", email: "1@1.com", address: nil, image: nil, wanted: true, sex: false, age: 20, year: 3, profile: nil, created_at: "2021-12-21 16:38:20", updated_at: "2021-12-21 16:38:39", myid: "1">
|
146
|
+
|
147
|
+
<User id: 32, provider: "email", uid: "3@3.com", allow_password_change: false, name: "3", email: "3@3.com", address: nil, image: nil, wanted: true, sex: false, age: 23, year: 5, profile: nil, created_at: "2021-12-21 16:40:09", updated_at: "2021-12-21 16:40:21", myid: "3">
|
148
|
+
|
149
|
+
<User id: 33, provider: "email", uid: "4@4.com", allow_password_change: false, name: "4", email: "4@4.com", address: nil, image: nil, wanted: true, sex: false, age: 30, year: 3, profile: nil, created_at: "2021-12-21 16:40:59", updated_at: "2021-12-21 16:41:14", myid: "4">
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
```
|
1
追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Rails ロジックの評価をお願いします。
|
test
CHANGED
@@ -14,6 +14,122 @@
|
|
14
14
|
|
15
15
|
取りたいデータは、
|
16
16
|
|
17
|
-
Userモデルからyearカラムが0以上且つ、addressカラムに入力されたaddressを一部含む且つ、wantedがtrue且つ、idが指定されたものを全て(今回の場合は25,26,27)。
|
17
|
+
Userモデルからyearカラムが0以上且つ、addressカラムに入力されたaddress(今回の場合はなし)を一部含む且つ、wantedがtrue且つ、idが指定されたものを全て(今回の場合は25,26,27)。
|
18
18
|
|
19
19
|
です。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
以下コードです。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
|
29
|
+
# User.rb
|
30
|
+
|
31
|
+
scope :year_gt, -> (lower_year) { where('year >= ?', lower_year) }
|
32
|
+
|
33
|
+
scope :address_like, -> (address) { where('address LIKE ?', "%#{address}%") }
|
34
|
+
|
35
|
+
scope :wanted_true, -> { where(wanted: true) }
|
36
|
+
|
37
|
+
scope :id_include, -> (ids) { where(id: ids )}
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
```
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
|
47
|
+
def index
|
48
|
+
|
49
|
+
lower_year = params[:lowerYear].to_i
|
50
|
+
|
51
|
+
address = params[:address]
|
52
|
+
|
53
|
+
host_skill_ids = params[:skillsId].map(&:to_i)
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
all_users = User.includes(:user_skills)
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
target_users_id = [] # skillが被っていないuserのidの配列
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
all_users.each do |user|
|
66
|
+
|
67
|
+
skills = user.user_skills
|
68
|
+
|
69
|
+
user_skill_ids = []
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
skills.each do |skill|
|
74
|
+
|
75
|
+
user_skill_ids.push(skill.skill_id)
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
user_skill_ids.push(host_skill_ids)
|
82
|
+
|
83
|
+
user_skill_ids.flatten!
|
84
|
+
|
85
|
+
mixed_skill_ids = user_skill_ids.uniq
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
if mixed_skill_ids.length == user_skill_ids.length
|
92
|
+
|
93
|
+
target_users_id.push(user.id)
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
# logger.debug(target_users_id) => [30, 32] OK。
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
users = Kaminari.paginate_array(User.all.year_gt(lower_year).address_like(address).wanted_true.id_include(target_users_id)).page(params[:page]).per(10)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
# logger.debug(users.inspect) => [] ここで取れていない
|
114
|
+
|
115
|
+
# ちなみにKaminariをはずして
|
116
|
+
|
117
|
+
# user = User.all.year_gt(lower_year).address_like(address).wanted_true.id_include(target_users_id) でも取れていないです。
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
@pagination = resources_with_pagination(users)
|
122
|
+
|
123
|
+
@users = users.as_json
|
124
|
+
|
125
|
+
@object = {
|
126
|
+
|
127
|
+
users: @users, kaminari: @pagination
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
render json: @object
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
```
|