質問編集履歴
2
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -164,7 +164,7 @@
|
|
164
164
|
|
165
165
|
```
|
166
166
|
|
167
|
-
render json: @questionnaires.to_json(include: {
|
167
|
+
render json: @questionnaires.to_json(include: {choices: {include: :answers}})
|
168
168
|
|
169
169
|
```
|
170
170
|
|
@@ -178,7 +178,7 @@
|
|
178
178
|
|
179
179
|
# 試し
|
180
180
|
|
181
|
-
render json: @questionnaires.to_json(include: [
|
181
|
+
render json: @questionnaires.to_json(include: [choices: {include: {methods: :count}}])
|
182
182
|
|
183
183
|
```
|
184
184
|
|
1
to_jsonのmethodsオプションの使い方が分からなかったので追加質問しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -40,23 +40,23 @@
|
|
40
40
|
|
41
41
|
孫:`Answerテーブル`
|
42
42
|
|
43
|
-
|id|user_id|choice_id|
|
43
|
+
|id|user_id|choice_id|questionnaire_id|
|
44
44
|
|
45
|
-
|:--|:--:|--:|
|
45
|
+
|:--|:--:|--:|--:|
|
46
46
|
|
47
|
-
|1|2|3|
|
47
|
+
|1|2|3|1|
|
48
48
|
|
49
|
-
|2|3|1|
|
49
|
+
|2|3|1|1|
|
50
50
|
|
51
|
-
|3|4|1|
|
51
|
+
|3|4|1|1|
|
52
52
|
|
53
|
-
|4|5|2|
|
53
|
+
|4|5|2|1|
|
54
54
|
|
55
|
-
|5|6|3|
|
55
|
+
|5|6|3|1|
|
56
56
|
|
57
|
-
|6|7|1|
|
57
|
+
|6|7|1|1|
|
58
58
|
|
59
|
-
|7|8|1|
|
59
|
+
|7|8|1|1|
|
60
60
|
|
61
61
|
|
62
62
|
|
@@ -103,3 +103,89 @@
|
|
103
103
|
Ruby 2.7.2
|
104
104
|
|
105
105
|
Rails 6.0.3.4
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
### 追記 新たな質問
|
112
|
+
|
113
|
+
下記のように、`to_json`では`methods`オプションが使えることが分かりました。
|
114
|
+
|
115
|
+
[参考](https://qiita.com/eggc/items/29a3c9a41d77227fb10a)
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
例
|
120
|
+
|
121
|
+
```Ruby
|
122
|
+
|
123
|
+
# メソッドの結果をカラムのようにして出力する
|
124
|
+
|
125
|
+
# オプションの methods を使います。たとえば Book に要約を出力するインスタンスメソッド summary が定義されている場合は下記のようにします。
|
126
|
+
|
127
|
+
Book.first.to_json(methods: [:summary])
|
128
|
+
|
129
|
+
```
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
こちらを参考に、下記のようにインスタンスメソッド(`countメソッド`)を定義しました。
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
```
|
140
|
+
|
141
|
+
# questionnaire.rb
|
142
|
+
|
143
|
+
class Questionnaire < ApplicationRecord
|
144
|
+
|
145
|
+
has_many :choices
|
146
|
+
|
147
|
+
has_many :answers
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
def count
|
152
|
+
|
153
|
+
Answer.group(:choice_id).count
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
```
|
162
|
+
|
163
|
+
しかし、このインスタンスメソッドを下記の`to_json`にどのようにして組み込むのかが分かりません。
|
164
|
+
|
165
|
+
```
|
166
|
+
|
167
|
+
render json: @questionnaires.to_json(include: {questionnaire_choices: {include: :answers}})
|
168
|
+
|
169
|
+
```
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
試しにこのようにしてみても、下記のようなエラーがでてしまいます。
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
```Ruby
|
178
|
+
|
179
|
+
# 試し
|
180
|
+
|
181
|
+
render json: @questionnaires.to_json(include: [questionnaire_choices: {include: {methods: :count}}])
|
182
|
+
|
183
|
+
```
|
184
|
+
|
185
|
+
```Ruby
|
186
|
+
|
187
|
+
# エラー
|
188
|
+
|
189
|
+
undefined method `serializable_hash' for :count:Symbol
|
190
|
+
|
191
|
+
```
|