質問編集履歴

5

エラー編集

2017/01/19 23:24

投稿

innjera
innjera

スコア132

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- 書籍やネット、teratailを活用しながら書いているつもりなのですが、`ActiveRecord`のエラー`Could not find the source association(s) :user in model Entry.`が出てしまい、解決できません。
1
+ 書籍やネット、teratailを活用しながら書いているつもりなのですが、`uninitialized constant Product::user `が出てしまい、解決できません。
2
2
 
3
3
 
4
4
 

4

修正

2017/01/19 23:24

投稿

innjera
innjera

スコア132

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,7 @@
1
+ 書籍やネット、teratailを活用しながら書いているつもりなのですが、`ActiveRecord`のエラー`Could not find the source association(s) :user in model Entry.`が出てしまい、解決できません。
2
+
3
+
4
+
1
5
  `User`と`Product`という2つのmodelに対し、2通りのassociationを定義しているのですが、そもそもそれが間違いでしょうか?
2
6
 
3
7
 
@@ -169,7 +173,3 @@
169
173
  end
170
174
 
171
175
  ```
172
-
173
-
174
-
175
- 書籍やネット、teratailを活用しながら書いているつもりなのですが、`ActiveRecord`のエラー`Could not find the source association(s) :user in model Entry.`が出てしまい、解決できません。誤りをご指摘頂けますと助かります。

3

修正

2017/01/19 23:06

投稿

innjera
innjera

スコア132

test CHANGED
@@ -1 +1 @@
1
- 多対多の関連付けでのActiveRecordエラー解決できない
1
+ 多対多の関連付け:共通するmodel間2通りassociation定義
test CHANGED
@@ -1,24 +1,24 @@
1
- 書籍やネット、teratailを活用しながら書いているつもりなのですが、`ActiveRecord`のエラー`Could not find the source association(s) :user in model Entry.`が出しまい、解決きません。誤りをご指摘頂けますと助ります。
1
+ `User`と`Product`という2つmodelに対し、2通りのassociationを定義しているのですがそもそもそれが間違いしょう
2
2
 
3
3
 
4
4
 
5
- `foreing key``外部キー`を必要以上に使いすぎなのでしょうか?
5
+ 即ち、`User`modelに`adviser`と`applicant`という2つの名前を定義し、`adviser`は`has many``product`を持ち、`product`と`applicant`は中間テブル'enty'を通して`has many through`で多対多の関係作って
6
6
 
7
7
 
8
8
 
9
9
  ###前提
10
10
 
11
- ユーザーがサービス出品し、ユーザー(購入者)そのサービスを受講きるというケースを想定
11
+ `adviser`と`applicant`別の`model`にないのは同一`User``adviser`にも`applicant`にもなれる為す。
12
12
 
13
- ・ユーザー出品とサービス : 1対多の関連付け
13
+ `adviser`と`product`の関係は1対多になります。(1人のユーザーが複数の'product'を出品する為
14
14
 
15
- サービスユーザー(購入者) : 多対多の関連付け
15
+ `applicant``product`の関係は多対多になります。(1つ`product`に、数の制限なく複数の`applicant`が申し込め、1人の`applicant`は他の商品にも申し込める為)
16
16
 
17
- *1つのサービスに複数のユーザー(購入者)が購入できる前提。
18
17
 
19
- ・ユーザーは出品者にも購入者にもなれる : 出品者と購入者は同じ`model`
20
18
 
21
- 出品者が出品したサビスと購入者が購入したサービスを分けて認識する。
19
+ すなわち、以下のイメ
20
+
21
+ `user(出品者)`(1) <-> (多) `product`(1)<->(多)`entry`(多) <->(1)`user(購入者)`
22
22
 
23
23
 
24
24
 
@@ -26,11 +26,7 @@
26
26
 
27
27
  ・`model`は`User`と`Product`と`Entry`(productとuserを多対多の関連付けで使用する中間テーブル)の3つ
28
28
 
29
- ・すなわち、以下のイメージ
30
29
 
31
- `user(出品者)`(1) <-> (多) `product`(1)<->(多)`entry`(多) <->(1)`user(購入者)`
32
-
33
-
34
30
 
35
31
  ###各モデルのテーブル(migration file) *単純化してます
36
32
 
@@ -100,11 +96,7 @@
100
96
 
101
97
  create_table :entries do |t|
102
98
 
103
- t.references :product_of_adviser, null: false
104
-
105
- t.references :product_of_applicant, null: false
106
-
107
- t.references :adviser, null: false
99
+ t.references :product, null: false
108
100
 
109
101
  t.references :applicant, null: false
110
102
 
@@ -116,11 +108,7 @@
116
108
 
117
109
 
118
110
 
119
- add_foreign_key :entries, :products, column: 'product_of_adviser_id'
120
-
121
- add_foreign_key :entries, :products, column: 'product_of_applicant_id'
122
-
123
- add_foreign_key :entries, :users, column: 'adviser_id'
111
+ add_foreign_key :entries, :products
124
112
 
125
113
  add_foreign_key :entries, :users, column: 'applicant_id'
126
114
 
@@ -142,13 +130,11 @@
142
130
 
143
131
  class User < ApplicationRecord
144
132
 
145
- has_many :entries, :class_name => 'Entry', :foreign_key => 'adviser_id'
133
+ has_many :products, :foreign_key => 'adviser_id'
146
134
 
147
- has_many :entries, :class_name => 'Entry', :foreign_key => 'applicant_id'
135
+ has_many :entries
148
136
 
149
- has_many :products_of_adviser, :through => :entries, source: :product, :foreign_key => 'adviser_id'
150
-
151
- has_many :products_of_applicant, :through => :entries, source: :product, :foreign_key => 'applicant_id'
137
+ has_many :products, through: :entries, :foreign_key => 'applicant_id'
152
138
 
153
139
  end
154
140
 
@@ -158,11 +144,15 @@
158
144
 
159
145
  class Product < ApplicationRecord
160
146
 
161
- has_many :entries
147
+ belongs_to :adviser, class_name: 'User'
162
148
 
163
- has_many :advisers, :through => :entries, :source => :user
149
+ has_many :entries
164
150
 
165
- has_many :applicants, :through => :entries, source: :user
151
+ has_many :applicants, through: :entries, source: :user
152
+
153
+ has_many :votes, dependent: :destroy
154
+
155
+ has_many :voters, through: :votes, source: :user
166
156
 
167
157
  end
168
158
 
@@ -172,18 +162,14 @@
172
162
 
173
163
  class Entry < ApplicationRecord
174
164
 
175
- belongs_to :adviser, :class_name => 'User'
165
+ belongs_to :applicant, class_name: 'User'
176
166
 
177
- belongs_to :applicant, :class_name => 'User'
167
+ belongs_to :product
178
-
179
- belongs_to :product_of_adviser, :class_name => 'Product'
180
-
181
- belongs_to :product_of_applicant, :class_name => 'Product'
182
168
 
183
169
  end
184
170
 
171
+ ```
185
172
 
186
173
 
187
174
 
188
-
189
- ```
175
+ 書籍やネット、teratailを活用しながら書いているつもりなのですが、`ActiveRecord`のエラー`Could not find the source association(s) :user in model Entry.`が出てしまい、解決できません。誤りをご指摘頂けますと助かります。

2

改善

2017/01/19 23:05

投稿

innjera
innjera

スコア132

test CHANGED
File without changes
test CHANGED
@@ -8,11 +8,13 @@
8
8
 
9
9
  ###前提
10
10
 
11
- ・ユーザーがサービスを出品し、別のユーザー(購入者)がそ購入できるというケースを想定
11
+ ・ユーザーがサービスを出品し、別のユーザー(購入者)がそのサービス受講できるというケースを想定
12
12
 
13
13
  ・ユーザー(出品者)とサービス : 1対多の関連付け
14
14
 
15
+ ・サービスとユーザー(購入者) : 多対多の関連付け
16
+
15
- ・サービスとユーザー(購入者) : 多対多の関連付け *1つのサービス(商品)に複数のユーザー(購入者)が購入できる前提。
17
+ *1つのサービスに複数のユーザー(購入者)が購入できる前提。
16
18
 
17
19
  ・ユーザーは出品者にも購入者にもなれる : 出品者と購入者は同じ`model`
18
20
 
@@ -26,7 +28,7 @@
26
28
 
27
29
  ・すなわち、以下のイメージ
28
30
 
29
- `user`(1) <-> (多)`entry`(多) <->(1) `product`
31
+ `user(出品者)`(1) <-> (多) `product`(1)<->(多)`entry`(多) <->(1)`user(購入者)`
30
32
 
31
33
 
32
34
 

1

誤字修正

2017/01/19 11:57

投稿

innjera
innjera

スコア132

test CHANGED
File without changes
test CHANGED
@@ -63,8 +63,6 @@
63
63
  class CreateProducts < ActiveRecord::Migration[5.0]
64
64
 
65
65
  def change
66
-
67
- create_table :products do |t|
68
66
 
69
67
  create_table :products do |t|
70
68