回答編集履歴
7
わ
test
CHANGED
@@ -44,4 +44,38 @@
|
|
44
44
|
|
45
45
|
|
46
46
|
|
47
|
+
追記
|
47
48
|
|
49
|
+
|
50
|
+
|
51
|
+
以下の様な感じでできそうです。
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
|
57
|
+
artist.ganres.clear
|
58
|
+
|
59
|
+
artist.genres << ジャンルのモデルの配列
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
以下でもいいかも知れません
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
|
71
|
+
artists.genre_ids = ジャンルのidの配列
|
72
|
+
|
73
|
+
```
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
6
ミス
test
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
アーティストのジャンルを編集する操作は、DBのテーブルの`artists_ganres`を変更することになりますよね?
|
2
2
|
|
3
3
|
ですから更新とはこのテーブルの行の削除or追加という事になります。
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
なので、create(destroy)を使うことになります。
|
8
4
|
|
9
5
|
|
10
6
|
|
@@ -18,7 +14,7 @@
|
|
18
14
|
|
19
15
|
```ruby
|
20
16
|
|
21
|
-
artist.genres
|
17
|
+
artist.genres << ganre
|
22
18
|
|
23
19
|
```
|
24
20
|
|
@@ -40,7 +36,7 @@
|
|
40
36
|
|
41
37
|
ganres.each do |ganre|
|
42
38
|
|
43
|
-
artist.genres
|
39
|
+
artist.genres << ganre
|
44
40
|
|
45
41
|
end
|
46
42
|
|
5
ミス
test
CHANGED
@@ -4,7 +4,11 @@
|
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
-
なので、createを使うことになります。
|
7
|
+
なので、create(destroy)を使うことになります。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
[参考](http://railsdoc.com/references/has_and_belongs_to_many)
|
8
12
|
|
9
13
|
|
10
14
|
|
@@ -32,7 +36,7 @@
|
|
32
36
|
|
33
37
|
artist.ganres.clear
|
34
38
|
|
35
|
-
ganres = ジャンルの
|
39
|
+
ganres = ジャンルのモデルの配列
|
36
40
|
|
37
41
|
ganres.each do |ganre|
|
38
42
|
|
4
a
test
CHANGED
@@ -1,12 +1,22 @@
|
|
1
1
|
アーティストのジャンルを編集する操作は、DBのテーブルの`artists_ganres`を変更することになりますよね?
|
2
|
+
|
3
|
+
ですから更新とはこのテーブルの行の削除or追加という事になります。
|
2
4
|
|
3
5
|
|
4
6
|
|
5
|
-
で、
|
7
|
+
なので、createを使うことになります。
|
6
8
|
|
7
9
|
|
8
10
|
|
11
|
+
こんな感じです。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
|
9
17
|
artist.genres.create
|
18
|
+
|
19
|
+
```
|
10
20
|
|
11
21
|
|
12
22
|
|
@@ -20,7 +30,7 @@
|
|
20
30
|
|
21
31
|
```ruby
|
22
32
|
|
23
|
-
artist.g
|
33
|
+
artist.ganres.clear
|
24
34
|
|
25
35
|
ganres = ジャンルのidの配列
|
26
36
|
|
3
わかりやすく
test
CHANGED
@@ -1,12 +1,4 @@
|
|
1
1
|
アーティストのジャンルを編集する操作は、DBのテーブルの`artists_ganres`を変更することになりますよね?
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
ですから、`collection`メソッドを利用して変更することになります。
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
[参考](http://railsdoc.com/references/has_and_belongs_to_many)
|
10
2
|
|
11
3
|
|
12
4
|
|
@@ -14,7 +6,7 @@
|
|
14
6
|
|
15
7
|
|
16
8
|
|
17
|
-
artist.
|
9
|
+
artist.genres.create
|
18
10
|
|
19
11
|
|
20
12
|
|
@@ -28,13 +20,13 @@
|
|
28
20
|
|
29
21
|
```ruby
|
30
22
|
|
31
|
-
artist.
|
23
|
+
artist.genres.clear
|
32
24
|
|
33
25
|
ganres = ジャンルのidの配列
|
34
26
|
|
35
27
|
ganres.each do |ganre|
|
36
28
|
|
37
|
-
artist.
|
29
|
+
artist.genres.create(ganre)
|
38
30
|
|
39
31
|
end
|
40
32
|
|
2
わかりやすくした
test
CHANGED
@@ -15,3 +15,31 @@
|
|
15
15
|
|
16
16
|
|
17
17
|
artist.collection.create
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
更新は一度関連を全部消してから必要な関係を順に作ればいいと思います。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
こんな感じかな?もっといいやり方があるかも知れません。
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
|
31
|
+
artist.collection.clear
|
32
|
+
|
33
|
+
ganres = ジャンルのidの配列
|
34
|
+
|
35
|
+
ganres.each do |ganre|
|
36
|
+
|
37
|
+
artist.collection.create(ganre)
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
```
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
1
ミス
test
CHANGED
@@ -2,20 +2,16 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
ですから、
|
5
|
+
ですから、`collection`メソッドを利用して変更することになります。
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
|
9
|
+
[参考](http://railsdoc.com/references/has_and_belongs_to_many)
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
-
|
13
|
+
で、それに対してcreateを順に発行すればよいです。
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
-
|
17
|
+
artist.collection.create
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
[参考:Rails Tutorial12章](http://railstutorial.jp/chapters/following_users?version=4.2#cha-following_users)
|