質問編集履歴
8
add
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
class User < ApplicationRecord
|
7
7
|
# name, user_codeカラムをもつ
|
8
8
|
|
9
|
-
has_many :items
|
9
|
+
has_many :items, foreign_key: :user_code, primary_key: :user_code, inverse_of: :user
|
10
10
|
end
|
11
11
|
|
12
12
|
class Item < ApplicationRecord
|
7
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,13 +4,13 @@
|
|
4
4
|
|
5
5
|
```
|
6
6
|
class User < ApplicationRecord
|
7
|
-
# nameカラムをもつ
|
7
|
+
# name, user_codeカラムをもつ
|
8
8
|
|
9
9
|
has_many :items
|
10
10
|
end
|
11
11
|
|
12
12
|
class Item < ApplicationRecord
|
13
|
-
# categoryカラムを持つ
|
13
|
+
# category, user_codeカラムを持つ
|
14
14
|
|
15
15
|
belongs_to :user, class_name: "User", foreign_key: :user_code, primary_key: :user_code, optional: true
|
16
16
|
|
6
error
title
CHANGED
File without changes
|
body
CHANGED
@@ -39,4 +39,11 @@
|
|
39
39
|
includesがうまくできていないようです。
|
40
40
|
```
|
41
41
|
#<Item::ActiveRecord_Relation:0x2ae7b0251a30>
|
42
|
+
```
|
43
|
+
|
44
|
+
エラーメッセージ
|
45
|
+
```
|
46
|
+
ActiveRecord::StatementInvalid:
|
47
|
+
PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block
|
48
|
+
: SELECT "items"."id" AS t0_r0, "items"."category" AS t0_r1, "items"."user_code" AS t0_r2, "items"."created_at" AS t0_r3, "items"."updated_at" AS t0_r4, "users"."id" AS t1_r0, "users"."user_code" AS t1_r1, "users"."name" AS t1_r2, , "users"."created_at" AS t1_r3, "users"."updated_at" AS t1_r4 FROM "items" LEFT OUTER JOIN "users" ON "users"."user_code" = "items"."user_code" WHERE ("users"."name" = $1 OR "items"."category" = $2) ORDER BY "items"."id" DESC LIMIT $3
|
42
49
|
```
|
5
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -36,6 +36,7 @@
|
|
36
36
|
SELECT * FROM items WHERE(users.name = 'tom')
|
37
37
|
```
|
38
38
|
のようになっているのですが、結果が以下のようになってしまいます。
|
39
|
+
includesがうまくできていないようです。
|
39
40
|
```
|
40
41
|
#<Item::ActiveRecord_Relation:0x2ae7b0251a30>
|
41
42
|
```
|
4
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -23,4 +23,19 @@
|
|
23
23
|
Item.includes(:user).where(category: "hobby").or(Item.includes(:user).where("users.name = 'tom'"))
|
24
24
|
```
|
25
25
|
|
26
|
-
どのようにしたらよいでしょうか?
|
26
|
+
どのようにしたらよいでしょうか?
|
27
|
+
|
28
|
+
|
29
|
+
追記
|
30
|
+
```
|
31
|
+
Item.includes(:user).where("users.name = 'tom'")
|
32
|
+
```
|
33
|
+
この部分がとれてないようです。
|
34
|
+
SQLとしては、
|
35
|
+
```
|
36
|
+
SELECT * FROM items WHERE(users.name = 'tom')
|
37
|
+
```
|
38
|
+
のようになっているのですが、結果が以下のようになってしまいます。
|
39
|
+
```
|
40
|
+
#<Item::ActiveRecord_Relation:0x2ae7b0251a30>
|
41
|
+
```
|
3
typo修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
↓のようなコードで試しているのですがうまくいきません。
|
21
21
|
|
22
22
|
```
|
23
|
-
Item.includes(:
|
23
|
+
Item.includes(:user).where(category: "hobby").or(Item.includes(:user).where("users.name = 'tom'"))
|
24
24
|
```
|
25
25
|
|
26
26
|
どのようにしたらよいでしょうか?
|
2
typo
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
class Item < ApplicationRecord
|
13
13
|
# categoryカラムを持つ
|
14
14
|
|
15
|
-
belongs_to user, class_name: "User", foreign_key: :user_code, primary_key: :user_code, optional: true
|
15
|
+
belongs_to :user, class_name: "User", foreign_key: :user_code, primary_key: :user_code, optional: true
|
16
16
|
|
17
17
|
end
|
18
18
|
```
|
1
typo
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
class Item < ApplicationRecord
|
13
13
|
# categoryカラムを持つ
|
14
14
|
|
15
|
-
belongs_to user, class_name: "
|
15
|
+
belongs_to user, class_name: "User", foreign_key: :user_code, primary_key: :user_code, optional: true
|
16
16
|
|
17
17
|
end
|
18
18
|
```
|