質問編集履歴

5

質問追加

2017/04/25 03:14

投稿

innjera
innjera

スコア132

test CHANGED
File without changes
test CHANGED
@@ -7,6 +7,42 @@
7
7
 
8
8
 
9
9
  ちなみに、associationを無理やりseedファイル内にて定義しなければ、`seed`ファイルは通ります。
10
+
11
+
12
+
13
+ また、`rails c`で同じコマンドを打つと、確り機能します。
14
+
15
+ ```ruby
16
+
17
+ [1] pry(main)> lesson = Lesson.find(42);0
18
+
19
+ Lesson Load (0.8ms) SELECT "lessons".* FROM "lessons" WHERE "lessons"."id" = $1 LIMIT $2 [["id", 42], ["LIMIT", 1]]
20
+
21
+ => 0
22
+
23
+ [2] pry(main)> situation = Situation.all.sample;0
24
+
25
+ Situation Load (0.6ms) SELECT "situations".* FROM "situations"
26
+
27
+ => 0
28
+
29
+ [3] pry(main)> lesson.situations << situation;0
30
+
31
+ (0.4ms) BEGIN
32
+
33
+ SQL (0.6ms) INSERT INTO "lessons_situations" ("situation_id", "lesson_id") VALUES ($1, $2) [["situation_id", 2], ["lesson_id", 42]]
34
+
35
+ (4.8ms) COMMIT
36
+
37
+ => 0
38
+
39
+ [4] pry(main)> lesson.situations.count
40
+
41
+ (1.3ms) SELECT COUNT(*) FROM "situations" INNER JOIN "lessons_situations" ON "situations"."id" = "lessons_situations"."situation_id" WHERE "lessons_situations"."lesson_id" = $1 [["lesson_id", 42]]
42
+
43
+ => 3
44
+
45
+ ```
10
46
 
11
47
 
12
48
 

4

タグ追加

2017/04/25 03:14

投稿

innjera
innjera

スコア132

test CHANGED
File without changes
test CHANGED
File without changes

3

質問改善

2017/04/25 02:20

投稿

innjera
innjera

スコア132

test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,8 @@
1
- 以下の通り関連付けを組んでおり、通常の動作(`new/create/edit/update`)には問題ありません。
1
+ 以下の通り`Lesson`と`Situation`の間に多対多の関連付けを組んでおり、通常の動作(`new/create/edit/update`)には問題ありません。
2
2
 
3
3
 
4
4
 
5
- 初期データとしてseedファイル内にて最初から関連付けがされた状態としたいのですが、`ActiveRecord::AssociationTypeMismatch:Situation(#70127423708740) expected, got NilClass(#70127366627340)`といったエラーが出てしまい解決できず、やり方をご教示頂けますと大変助かります。
5
+ 初期データとしてseedファイル内にて最初から関連付けがされた状態としたいのですが、`lesson.situations << Situation.all.sample`を`Lesson`modelの`seed`ファイルに記載したところ、`ActiveRecord::AssociationTypeMismatch:Situation(#70127423708740) expected, got NilClass(#70127366627340)`といったエラーが出てしまい解決できず、やり方をご教示頂けますと大変助かります。
6
6
 
7
7
 
8
8
 

2

title修正

2017/04/25 01:46

投稿

innjera
innjera

スコア132

test CHANGED
@@ -1 +1 @@
1
- 関連付けをseedファイル内で初期データとして設定する方法
1
+ Rails : has_manyをseedファイル内で初期データとして設定する方法
test CHANGED
File without changes

1

コード追加

2017/04/25 01:44

投稿

innjera
innjera

スコア132

test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  ###前提
14
14
 
15
- `model`は`Lesson`と`Situation`の2つで、各々の`table`含む`model`は以下の通りです。
15
+ `model`は`Lesson`と`Situation`の2つで、各々の`table`含む`model`は以下の通りです。また、`Lesson`と`Situation`の間にjoin tableを組んでます。(以下コード内に記載)
16
16
 
17
17
  ```rby
18
18
 
@@ -49,6 +49,24 @@
49
49
  class Situation < ApplicationRecord
50
50
 
51
51
  has_and_belongs_to_many :lessons
52
+
53
+ end
54
+
55
+
56
+
57
+ class CreateJoinTableSituationLesson < ActiveRecord::Migration[5.0]
58
+
59
+ def change
60
+
61
+ create_join_table :situations, :lessons do |t|
62
+
63
+ # t.index [:situation_id, :lesson_id]
64
+
65
+ # t.index [:lesson_id, :situation_id]
66
+
67
+ end
68
+
69
+ end
52
70
 
53
71
  end
54
72