質問編集履歴
3
model部分を全体的に記述し、どのように関連づけてコメント作成したいのかイメージしやすくするために、エクセルで表を作成してみました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
現在facebookのようなアプリをrailsで作成しようとしています。
|
2
2
|
|
3
|
+
イメージとしてはこのように、データベースで関連付ければ良いのかと考えました。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
https://gyazo.com/735ed14dd8fbf57b6588396771dc5f61
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
コメントの1つ目のuser_idは誰の記事かを特定するためのもの、
|
12
|
+
|
13
|
+
2つ目のuser_idは誰がコメントしたかを特定するためのものです。
|
14
|
+
|
15
|
+
|
16
|
+
|
3
17
|
そこで、
|
4
18
|
|
5
19
|
会員は複数の記事を持ち、
|
@@ -10,9 +24,61 @@
|
|
10
24
|
|
11
25
|
```
|
12
26
|
|
13
|
-
|
27
|
+
記事にはユーザーの外部キー(user_id)を
|
28
|
+
|
14
|
-
|
29
|
+
class CreateMicroposts < ActiveRecord::Migration
|
30
|
+
|
31
|
+
def change
|
32
|
+
|
33
|
+
create_table :microposts do |t|
|
34
|
+
|
35
|
+
t.string :content
|
36
|
+
|
37
|
+
t.references :user, index: true
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
t.timestamps null: false
|
42
|
+
|
43
|
+
t.index [:user_id, :created_at ]
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
|
15
|
-
|
53
|
+
コメントにはユーザー(user_id)と記事(micropost_id)の外部キーを
|
54
|
+
|
55
|
+
class CreateComments < ActiveRecord::Migration
|
56
|
+
|
57
|
+
def change
|
58
|
+
|
59
|
+
create_table :comments do |t|
|
60
|
+
|
61
|
+
t.references :user, index: true
|
62
|
+
|
63
|
+
t.references :micropost, index: true
|
64
|
+
|
65
|
+
t.string :content
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
t.timestamps null: false
|
70
|
+
|
71
|
+
t.index [:user_id,:micropost_id ,:created_at ]
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
16
82
|
|
17
83
|
```
|
18
84
|
|
@@ -28,11 +94,19 @@
|
|
28
94
|
|
29
95
|
```
|
30
96
|
|
97
|
+
def show
|
98
|
+
|
31
|
-
@user = User.find(params[:id])
|
99
|
+
@user = User.find(params[:id])
|
32
|
-
|
100
|
+
|
33
|
-
@microposts = @user.microposts
|
101
|
+
@microposts = @user.microposts
|
34
|
-
|
102
|
+
|
35
|
-
@comment = @microposts.comments.build
|
103
|
+
@comment = @microposts.comments.build
|
104
|
+
|
105
|
+
@comments = @microposts.comments
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
|
36
110
|
|
37
111
|
```
|
38
112
|
|
@@ -42,7 +116,23 @@
|
|
42
116
|
|
43
117
|
```
|
44
118
|
|
119
|
+
def create
|
120
|
+
|
45
|
-
@comment = current_user.mic
|
121
|
+
@comment = current_user.microposts.comments.build(comment_params)
|
122
|
+
|
123
|
+
if @comment.save
|
124
|
+
|
125
|
+
flash[:success] = "コメントしました。"
|
126
|
+
|
127
|
+
redirect_to root_path
|
128
|
+
|
129
|
+
else
|
130
|
+
|
131
|
+
render 'static_pages/home'
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
46
136
|
|
47
137
|
```
|
48
138
|
|
@@ -112,30 +202,62 @@
|
|
112
202
|
|
113
203
|
```
|
114
204
|
|
205
|
+
class User < ActiveRecord::Base
|
206
|
+
|
207
|
+
has_one :member
|
208
|
+
|
115
|
-
has_many :microposts
|
209
|
+
has_many :microposts
|
116
210
|
|
117
211
|
has_many :comments
|
118
212
|
|
213
|
+
|
214
|
+
|
215
|
+
has_secure_password
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
accepts_nested_attributes_for :member
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
|
228
|
+
|
119
229
|
```
|
120
230
|
|
121
231
|
記事のモデルはこうです。
|
122
232
|
|
123
233
|
```
|
124
234
|
|
235
|
+
class Micropost < ActiveRecord::Base
|
236
|
+
|
125
237
|
belongs_to :user
|
126
238
|
|
127
239
|
has_many :comments
|
128
240
|
|
241
|
+
end
|
242
|
+
|
243
|
+
|
244
|
+
|
129
245
|
```
|
130
246
|
|
131
247
|
コメントモデルではこうです。
|
132
248
|
|
133
249
|
```
|
134
250
|
|
251
|
+
class Comment < ActiveRecord::Base
|
252
|
+
|
135
|
-
belongs_to :micropost
|
253
|
+
belongs_to :micropost
|
136
254
|
|
137
255
|
belongs_to :user
|
138
256
|
|
257
|
+
end
|
258
|
+
|
259
|
+
|
260
|
+
|
139
261
|
```
|
140
262
|
|
141
263
|
現在はこのような関連を作成しています。
|
2
ソースコード部分を見やすくしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,9 +8,15 @@
|
|
8
8
|
|
9
9
|
その際に、
|
10
10
|
|
11
|
-
|
11
|
+
```
|
12
12
|
|
13
|
+
Micropostには、ユーザの外部キー(user_id)
|
14
|
+
|
13
|
-
|
15
|
+
Commentには、ユーザーの外部キー(user_id)と記事の外部キー(micropost_id)
|
16
|
+
|
17
|
+
```
|
18
|
+
|
19
|
+
を持っています。
|
14
20
|
|
15
21
|
それらを利用して、
|
16
22
|
|
@@ -18,31 +24,49 @@
|
|
18
24
|
|
19
25
|
したいのですが、
|
20
26
|
|
21
|
-
|
27
|
+
ユーザーコントローラーで
|
22
28
|
|
29
|
+
```
|
30
|
+
|
23
|
-
|
31
|
+
@user = User.find(params[:id])
|
24
32
|
|
25
33
|
@microposts = @user.microposts
|
26
34
|
|
27
|
-
@comment = @microposts.comments.build
|
35
|
+
@comment = @microposts.comments.build
|
36
|
+
|
37
|
+
```
|
28
38
|
|
29
39
|
と記述し、
|
30
40
|
|
31
|
-
|
41
|
+
コメントコントローラ内で、
|
32
42
|
|
43
|
+
```
|
44
|
+
|
33
|
-
|
45
|
+
@comment = current_user.mictoposts.comments.build(comment_params)
|
46
|
+
|
47
|
+
```
|
34
48
|
|
35
49
|
と記述し、起動してみますと、
|
36
50
|
|
37
|
-
**NoMethodError (undefined method `comments' for #<Micropost::ActiveRecord_Associations_CollectionProxy:0x007f3c1e1ac8e8>):
|
38
51
|
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
NoMethodError (undefined method `comments' for #<Micropost::ActiveRecord_Associations_CollectionProxy:0x007f3c1e1ac8e8>):
|
56
|
+
|
57
|
+
```
|
58
|
+
|
39
|
-
とエラー
|
59
|
+
とエラーが発生してしまいます。
|
40
60
|
|
41
61
|
|
42
62
|
|
43
63
|
ですが、
|
44
64
|
|
45
|
-
|
65
|
+
ユーザーコントローラー内で
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
```
|
46
70
|
|
47
71
|
@user = User.find(params[:id])
|
48
72
|
|
@@ -50,13 +74,19 @@
|
|
50
74
|
|
51
75
|
@comment = @user.comments.build
|
52
76
|
|
53
|
-
@comments = @user.comments
|
77
|
+
@comments = @user.comments
|
78
|
+
|
79
|
+
```
|
54
80
|
|
55
81
|
|
56
82
|
|
57
|
-
|
83
|
+
コメントコントローラー内で
|
58
84
|
|
85
|
+
```
|
86
|
+
|
59
|
-
@comment = current_user.comments.build(comment_params)
|
87
|
+
@comment = current_user.comments.build(comment_params)
|
88
|
+
|
89
|
+
```
|
60
90
|
|
61
91
|
と記述しすると
|
62
92
|
|
@@ -78,22 +108,34 @@
|
|
78
108
|
|
79
109
|
これが、
|
80
110
|
|
81
|
-
|
111
|
+
ユーザーモデルです。
|
112
|
+
|
113
|
+
```
|
82
114
|
|
83
115
|
has_many :microposts
|
84
116
|
|
85
117
|
has_many :comments
|
86
118
|
|
119
|
+
```
|
120
|
+
|
87
121
|
記事のモデルはこうです。
|
122
|
+
|
123
|
+
```
|
88
124
|
|
89
125
|
belongs_to :user
|
90
126
|
|
91
127
|
has_many :comments
|
92
128
|
|
129
|
+
```
|
130
|
+
|
93
131
|
コメントモデルではこうです。
|
132
|
+
|
133
|
+
```
|
94
134
|
|
95
135
|
belongs_to :micropost
|
96
136
|
|
97
|
-
belongs_to :user
|
137
|
+
belongs_to :user
|
138
|
+
|
139
|
+
```
|
98
140
|
|
99
141
|
現在はこのような関連を作成しています。
|
1
ソースコード部分等を見やすくしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,9 +8,9 @@
|
|
8
8
|
|
9
9
|
その際に、
|
10
10
|
|
11
|
-
Micropostには、ユーザの外部キー(user_id)
|
11
|
+
**Micropostには、ユーザの外部キー(user_id)**
|
12
12
|
|
13
|
-
Commentには、ユーザーの外部キー(user_id)と記事の外部キー(micropost_id)を持っています。
|
13
|
+
**Commentには、ユーザーの外部キー(user_id)と記事の外部キー(micropost_id)**を持っています。
|
14
14
|
|
15
15
|
それらを利用して、
|
16
16
|
|
@@ -18,31 +18,31 @@
|
|
18
18
|
|
19
19
|
したいのですが、
|
20
20
|
|
21
|
-
ユーザーコントローラーで
|
21
|
+
**ユーザーコントローラーで**
|
22
22
|
|
23
|
-
@user = User.find(params[:id])
|
23
|
+
**@user = User.find(params[:id])
|
24
24
|
|
25
25
|
@microposts = @user.microposts
|
26
26
|
|
27
|
-
@comment = @microposts.comments.build
|
27
|
+
@comment = @microposts.comments.build**
|
28
28
|
|
29
29
|
と記述し、
|
30
30
|
|
31
|
-
コメントコントローラ内で、
|
31
|
+
**コメントコントローラ内で、**
|
32
32
|
|
33
|
-
@comment = current_user.mictoposts.comments.build(comment_params)
|
33
|
+
**@comment = current_user.mictoposts.comments.build(comment_params)**
|
34
34
|
|
35
35
|
と記述し、起動してみますと、
|
36
36
|
|
37
|
-
NoMethodError (undefined method `comments' for #<Micropost::ActiveRecord_Associations_CollectionProxy:0x007f3c1e1ac8e8>):
|
37
|
+
**NoMethodError (undefined method `comments' for #<Micropost::ActiveRecord_Associations_CollectionProxy:0x007f3c1e1ac8e8>):
|
38
38
|
|
39
|
-
とエラーが発生してしまいます。
|
39
|
+
とエラー**が発生してしまいます。
|
40
40
|
|
41
41
|
|
42
42
|
|
43
43
|
ですが、
|
44
44
|
|
45
|
-
ユーザーコントローラー内で
|
45
|
+
**ユーザーコントローラー内で
|
46
46
|
|
47
47
|
@user = User.find(params[:id])
|
48
48
|
|
@@ -50,13 +50,13 @@
|
|
50
50
|
|
51
51
|
@comment = @user.comments.build
|
52
52
|
|
53
|
-
@comments = @user.comments
|
53
|
+
@comments = @user.comments**
|
54
54
|
|
55
55
|
|
56
56
|
|
57
|
-
コメントコントローラー内で
|
57
|
+
**コメントコントローラー内で
|
58
58
|
|
59
|
-
@comment = current_user.comments.build(comment_params)
|
59
|
+
@comment = current_user.comments.build(comment_params)**
|
60
60
|
|
61
61
|
と記述しすると
|
62
62
|
|
@@ -78,7 +78,7 @@
|
|
78
78
|
|
79
79
|
これが、
|
80
80
|
|
81
|
-
ユーザーモデルです。
|
81
|
+
**ユーザーモデルです。
|
82
82
|
|
83
83
|
has_many :microposts
|
84
84
|
|
@@ -94,6 +94,6 @@
|
|
94
94
|
|
95
95
|
belongs_to :micropost
|
96
96
|
|
97
|
-
belongs_to :user
|
97
|
+
belongs_to :user**
|
98
98
|
|
99
99
|
現在はこのような関連を作成しています。
|