質問編集履歴

1

質問の内容をシンプルにしました。

2017/10/29 08:16

投稿

koume
koume

スコア458

test CHANGED
@@ -1 +1 @@
1
- Railsで1対1の係にあるテーブルに保存させるタイミングについて教えてください。
1
+ Railsで関連付けしたテーブルにレコ作成させる方法を教えてください。
test CHANGED
@@ -2,174 +2,82 @@
2
2
 
3
3
 
4
4
 
5
- 表示の流れは以下のようになります。
5
+ やりたいことは以下のようになります。
6
6
 
7
- 1,新規アカウント情報をcustomersテーブルに登録します。
7
+ 1,新規ユーザーがアカウント情報をcustomersテーブルに登録します。
8
8
 
9
- 2,あらためてログインします。
10
-
11
- ・top画面が表示されます。
12
-
13
- ・項目ごと表示させることができます
9
+ 2,customersテーブルのデータを取り出し関連付けされた別のmembersテーブル新規にレコ作成し保存させたい
14
10
 
15
11
 
16
12
 
17
- ときに自分のアウント情報加工したデータを表示させたので、どのタイミングで関連付けさたテーブルに
13
+ customersテーブルのカラムteamとうカラムがありま。新規ユーザーはそこに「A」「B」、「C」のうちどれかを
18
14
 
19
- 外部キを設定て保存ればいいのでしょうか?
15
+ 選んでフォムに打ち込み登録
20
16
 
21
17
 
22
18
 
23
- topコントローラに記述してtop画面を開くと同時に登録させるというやりかたでできるのでしょうか?
24
-
25
- イメージは以下になります。(pointテーブルに登録する場合)
26
-
27
19
  ```ここに言語を入力
28
20
 
29
- top_controller.rb
21
+ customer = Customer.find_by(id: current_customer.id)
30
22
 
23
+ team = customer.tema
31
24
 
25
+ if team == "A"
32
26
 
33
- class Customer::TopController < Customer::Base
27
+ value = current_customer.id + 100
34
28
 
35
- skip_before_action :authorize
29
+ set = customer.build_member(number: value)
36
30
 
31
+ set.save
37
32
 
33
+ elsif team == "B"
38
34
 
39
- #ここにpointテーブルにデータを保存するコードを記述していく。
35
+ value = current_customer.id + 200
40
36
 
41
- id_get = Point.where(customer_id: current_customer.id)
37
+ set = customer.build_member(number: value)
42
38
 
39
+ set.save
43
40
 
41
+ else
44
42
 
45
- if id_get.empty?
43
+ value = current_customer.id + 300
46
44
 
47
- customer = Customer.find_by(id: current_customer.id)
48
-
49
- point = customer.number
50
-
51
- value = point + 1000
52
-
53
- set = customer.build_point(number: value)
45
+ set = customer.build_member(number: value)
54
46
 
55
47
  set.save
56
48
 
57
49
  end
58
50
 
51
+ ```
59
52
 
53
+ このようにコントローラに記述すれば本人がログインした場合はmembersテーブルにレコードは保存されると思いますが、
60
54
 
61
- #ログイン成功時に表示るページのコード
55
+ 本人がログインしない限り登録されないと考えていま
62
56
 
63
- def index
57
+ この状況で他のメンバーが自分のチームメンバーを表示しても新規の人のデータが登録されていないテーブルを表示してしまうと
64
58
 
65
- if current_customer
59
+ 思うのでやり方がわからなくて困っています。
66
-
67
- customer = Customer.find_by(id: current_customer.id)
68
-
69
- end
70
-
71
-
72
-
73
- if current_customer
74
-
75
- if Member.find_by(customer_id: customer.id)
76
-
77
- render action: 'dashboard_b'
78
-
79
- else
80
-
81
- render action: 'dashboard_a'
82
-
83
- end
84
-
85
- else
86
-
87
- render action: 'index'
88
-
89
- end
90
-
91
- end
92
-
93
- end
94
60
 
95
61
 
96
62
 
97
- ```
63
+ 新規にユーザーが登録した場合に本人がログインする、しないにかかわらず、membersテーブルに外部キーを設置して
98
64
 
99
- それともちらの記述の方がいいのでしょうか?
65
+ レコードを新規に作るにはどに、どように記述すればいいのでしょうか?
100
-
101
- ```ここに言語を入力
102
-
103
- top_controller.rb
104
66
 
105
67
 
106
68
 
107
- class Customer::TopController < Customer::Base
69
+ これがベストなのですが・・・
108
70
 
71
+ customers_controllerのcreateアクションに上記コードを記述すれば新規にユーザー登録したと同時に
72
+
109
- skip_before_action :authorize
73
+ membersテーブルにも保存されるのでしょうか?
110
74
 
111
75
 
112
76
 
113
-  def index
114
-
115
-   #ここにpointテーブルにデータを保存するコードを記述していく。
116
-
117
-  id_get = Point.where(customer_id: current_customer.id)
77
+ 表現がおかしいかもしれませんが裏側でmembersテーブルにレコードの新規作成をさせたいのです。
118
78
 
119
79
 
120
80
 
121
-  if id_get.empty?
81
+ 新規にユーザー登録したときにmembersテーブルに外部キー(customer_id)に値を入れてテーブルを作りたくて
122
82
 
123
-  customer = Customer.find_by(id: current_customer.id)
124
-
125
-  point = customer.number
126
-
127
-  value = point + 1000
128
-
129
-  set = customer.build_point(number: value)
130
-
131
-  set.save
132
-
133
-  end
134
-
135
-
136
-
137
-   if current_customer
138
-
139
- customer = Customer.find_by(id: current_customer.id)
140
-
141
- end
142
-
143
-
144
-
145
- if current_customer
146
-
147
- if Member.find_by(customer_id: customer.id)
148
-
149
- render action: 'dashboard_b'
83
+ 悩んでいます。どなたか教えていただけないでしょうか?宜しくお願いします。
150
-
151
- else
152
-
153
- render action: 'dashboard_a'
154
-
155
- end
156
-
157
- else
158
-
159
- render action: 'index'
160
-
161
- end
162
-
163
- end
164
-
165
- end
166
-
167
-
168
-
169
- ```
170
-
171
- このようにtpo_contorollerに記述すればTop画面でログインフォームを表示させる前にpointテーブルにログイン中の
172
-
173
- customerと関連付けされたレコードが登録されると思うのですがこの記述で、ログイン後にpointページを表示させたときに
174
-
175
- value = point + 1000の値を表示させることはできるのでしょうか?