質問編集履歴

8

add

2019/08/31 10:25

投稿

indi
indi

スコア26

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
 
16
16
 
17
- has_many :items
17
+ has_many :items, foreign_key: :user_code, primary_key: :user_code, inverse_of: :user
18
18
 
19
19
  end
20
20
 

7

追記

2019/08/31 10:25

投稿

indi
indi

スコア26

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  class User < ApplicationRecord
12
12
 
13
- # nameカラムをもつ
13
+ # name, user_codeカラムをもつ
14
14
 
15
15
 
16
16
 
@@ -22,7 +22,7 @@
22
22
 
23
23
  class Item < ApplicationRecord
24
24
 
25
- # categoryカラムを持つ
25
+ # category, user_codeカラムを持つ
26
26
 
27
27
 
28
28
 

6

error

2019/08/31 09:05

投稿

indi
indi

スコア26

test CHANGED
File without changes
test CHANGED
@@ -81,3 +81,17 @@
81
81
  #<Item::ActiveRecord_Relation:0x2ae7b0251a30>
82
82
 
83
83
  ```
84
+
85
+
86
+
87
+ エラーメッセージ
88
+
89
+ ```
90
+
91
+ ActiveRecord::StatementInvalid:
92
+
93
+ PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block
94
+
95
+ : 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
96
+
97
+ ```

5

追記

2019/08/31 07:59

投稿

indi
indi

スコア26

test CHANGED
File without changes
test CHANGED
@@ -74,6 +74,8 @@
74
74
 
75
75
  のようになっているのですが、結果が以下のようになってしまいます。
76
76
 
77
+ includesがうまくできていないようです。
78
+
77
79
  ```
78
80
 
79
81
  #<Item::ActiveRecord_Relation:0x2ae7b0251a30>

4

追記

2019/08/31 07:51

投稿

indi
indi

スコア26

test CHANGED
File without changes
test CHANGED
@@ -49,3 +49,33 @@
49
49
 
50
50
 
51
51
  どのようにしたらよいでしょうか?
52
+
53
+
54
+
55
+
56
+
57
+ 追記
58
+
59
+ ```
60
+
61
+ Item.includes(:user).where("users.name = 'tom'")
62
+
63
+ ```
64
+
65
+ この部分がとれてないようです。
66
+
67
+ SQLとしては、
68
+
69
+ ```
70
+
71
+ SELECT * FROM items WHERE(users.name = 'tom')
72
+
73
+ ```
74
+
75
+ のようになっているのですが、結果が以下のようになってしまいます。
76
+
77
+ ```
78
+
79
+ #<Item::ActiveRecord_Relation:0x2ae7b0251a30>
80
+
81
+ ```

3

typo修正

2019/08/31 07:49

投稿

indi
indi

スコア26

test CHANGED
File without changes
test CHANGED
@@ -42,7 +42,7 @@
42
42
 
43
43
  ```
44
44
 
45
- Item.includes(:users).where(category: "hobby").or(Item.includes(:users).where("users.name = 'tom'"))
45
+ Item.includes(:user).where(category: "hobby").or(Item.includes(:user).where("users.name = 'tom'"))
46
46
 
47
47
  ```
48
48
 

2

typo

2019/08/31 07:40

投稿

indi
indi

スコア26

test CHANGED
File without changes
test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
 
28
28
 
29
- belongs_to user, class_name: "User", foreign_key: :user_code, primary_key: :user_code, optional: true
29
+ belongs_to :user, class_name: "User", foreign_key: :user_code, primary_key: :user_code, optional: true
30
30
 
31
31
 
32
32
 

1

typo

2019/08/31 07:33

投稿

indi
indi

スコア26

test CHANGED
File without changes
test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
 
28
28
 
29
- belongs_to user, class_name: "BookShop", foreign_key: :user_code, primary_key: :user_code, optional: true
29
+ belongs_to user, class_name: "User", foreign_key: :user_code, primary_key: :user_code, optional: true
30
30
 
31
31
 
32
32