質問編集履歴

2

誤字

2017/01/23 14:31

投稿

nk117
nk117

スコア31

test CHANGED
File without changes
test CHANGED
@@ -1,469 +1,5 @@
1
- ###前提・実現したい
1
+ Railsでサイトを作っています。各物件の詳細ページから、お問合わせ(内覧予約)ができるようにしたいのですが、その際に「本id」「お問合わせ内容」を紐付けしたいと考えています。
2
2
 
3
- Railsで不動産情報サイトを作っています。各物件の詳細ペジから問合わせ(覧予約)ができるようにしたいのですが、その際に物件id」と「お問合わせ内容」を紐付けたいと考えています。
3
+ 物件タを「books」、問合わせ内容をrequires」としてDBに保存しています。
4
4
 
5
- 物件データを「buildings」、問い合わせ内容を「requires」としてDBに保存しています。
6
-
7
- 内覧予約の際に「buildingsテーブルのid」を、「Requiresテーブルのbuilding_id」としてDBに取得するのに、良い方法をご存知でしたらご教授頂けませんでしょうか。何卒宜しくお願い申し上げます。
5
+ 内覧予約の際に「booksテーブルのid」を、「Requiresテーブルのbooks_id」としてDBに取得するのに、良い方法をご存知でしたらご教授頂けませんでしょうか。何卒宜しくお願い申し上げます。
8
-
9
-
10
-
11
- ###発生している問題・エラーメッセージ
12
-
13
-
14
-
15
- ```
16
-
17
- エラーは出ておりません。buildingsの左端のカラムが物件idです。
18
-
19
-
20
-
21
- ```
22
-
23
- ![参考画像:buildingsのDB](540deae5319c4cc8c21cd53c31f75c00.png)
24
-
25
- ```
26
-
27
- requireの右端のカラムのbuilding_idだけ取得できていない状況です。
28
-
29
- ```
30
-
31
- ![参考画像:requireのDB](0ef54ded4bed4d29007c6e322c92d45d.png)
32
-
33
- ###DB
34
-
35
- ```
36
-
37
- <db/migrate/create_requires.rb>
38
-
39
-
40
-
41
- class CreateRequires < ActiveRecord::Migration
42
-
43
- def change
44
-
45
- create_table :requires do |t|
46
-
47
- t.string :name
48
-
49
- t.string :tel
50
-
51
- t.string :email
52
-
53
- t.date :require_date
54
-
55
- t.time :require_time
56
-
57
- t.string :gender
58
-
59
- t.string :age
60
-
61
- t.string :occupation
62
-
63
- t.integer :building_id
64
-
65
- t.timestamps null: false
66
-
67
- end
68
-
69
- end
70
-
71
- end
72
-
73
-
74
-
75
- ```
76
-
77
- ```
78
-
79
- <db/migrate/create_buildings.rb>
80
-
81
-
82
-
83
- class CreateBuildings < ActiveRecord::Migration
84
-
85
- def change
86
-
87
- create_table :buildings do |t|
88
-
89
- t.string :name
90
-
91
- t.integer :rent
92
-
93
- t.integer :manage
94
-
95
- t.integer :deposit
96
-
97
- t.integer :key_money
98
-
99
- t.string :address
100
-
101
- t.string :ward
102
-
103
- t.integer :user_id
104
-
105
- t.string :building_image
106
-
107
- t.decimal :latitude
108
-
109
- t.decimal :longitude
110
-
111
- t.timestamps null: false
112
-
113
- end
114
-
115
- end
116
-
117
- end
118
-
119
-
120
-
121
- ```
122
-
123
- ###Models
124
-
125
- ```
126
-
127
- <app/models/require.rb>
128
-
129
-
130
-
131
- class Require < ActiveRecord::Base
132
-
133
- belongs_to :building , foreign_key: 'building_id' #←変化なし
134
-
135
- end
136
-
137
- ```
138
-
139
- ```
140
-
141
- <app/models/buildings.rb>
142
-
143
-
144
-
145
- class Building < ActiveRecord::Base
146
-
147
- validates :name,presence: true
148
-
149
- validates :rent,presence: true
150
-
151
- validates :manage,presence: true
152
-
153
- validates :deposit,presence: true
154
-
155
- validates :key_money,presence: true
156
-
157
- validates :address,presence: true
158
-
159
- validates :user_id,presence: true
160
-
161
- belongs_to :user
162
-
163
- has_many :requires
164
-
165
- mount_uploader :building_image, BuildingImageUploader
166
-
167
- geocoded_by :address
168
-
169
- after_validation :geocode
170
-
171
- end
172
-
173
- ```
174
-
175
- ###Controller
176
-
177
- ```
178
-
179
- <app/requires_controller.rb>
180
-
181
-
182
-
183
- class RequiresController < InheritedResources::Base
184
-
185
-
186
-
187
- def create
188
-
189
- @require = Require.new(require_params)
190
-
191
-
192
-
193
- respond_to do |format|
194
-
195
- if @require.save
196
-
197
- format.html { redirect_to @require, notice: '投稿されました' }
198
-
199
- format.json { render :show, status: :created, location: @require }
200
-
201
- else
202
-
203
- format.html { render :new }
204
-
205
- format.json { render json: @require.errors, status: :unprocessable_entity }
206
-
207
- end
208
-
209
- end
210
-
211
- end
212
-
213
-
214
-
215
- private
216
-
217
-
218
-
219
-
220
-
221
- def require_params
222
-
223
- params.require(:require).permit(:name, :tel, :email, :require_date, :require_time, :gender, :age, :occupation,)
224
-
225
- end
226
-
227
- end
228
-
229
- ```
230
-
231
- ```
232
-
233
- <app/controllers/buildings.controller.rb>
234
-
235
-
236
-
237
- class BuildingsController < ApplicationController
238
-
239
- before_action :authenticate_user!, only: [ :new, :edit]
240
-
241
- before_action :correct_user, only: [ :update]
242
-
243
- before_action :set_building, only: [ :update, :destroy]
244
-
245
-
246
-
247
- # GET /buildings
248
-
249
- # GET /buildings.json
250
-
251
- def index
252
-
253
- #@buildings = Building.all
254
-
255
- @q = Building.search(params[:q])
256
-
257
- @buildings = @q.result(distinct: true)
258
-
259
- end
260
-
261
-
262
-
263
- # GET /buildings/1
264
-
265
- # GET /buildings/1.json
266
-
267
- def show
268
-
269
- end
270
-
271
-
272
-
273
- # GET /buildings/new
274
-
275
- def new
276
-
277
- @building = Building.new
278
-
279
- end
280
-
281
-
282
-
283
- # GET /buildings/1/edit
284
-
285
- def edit
286
-
287
- @building = Building.find(params[:id])
288
-
289
- @user = User.find(current_user.id)
290
-
291
- end
292
-
293
-
294
-
295
-
296
-
297
- def find
298
-
299
- @building = Building.find(params[:id])
300
-
301
- @hash = Gmaps4rails.build_markers(@building) do |building, marker|
302
-
303
- marker.lat building.latitude
304
-
305
- marker.lng building.longitude
306
-
307
- end
308
-
309
- end
310
-
311
-
312
-
313
-
314
-
315
- # POST /buildings
316
-
317
- # POST /buildings.json
318
-
319
- def create
320
-
321
- @building = Building.new(building_params)
322
-
323
- @building.user_id = current_user.id
324
-
325
-
326
-
327
-
328
-
329
- respond_to do |format|
330
-
331
- if @building.save
332
-
333
- format.html { redirect_to @building, notice: '投稿されました' }
334
-
335
- format.json { render :show, status: :created, location: @building }
336
-
337
- else
338
-
339
- format.html { render :new }
340
-
341
- format.json { render json: @building.errors, status: :unprocessable_entity }
342
-
343
- end
344
-
345
- end
346
-
347
- end
348
-
349
-
350
-
351
- # PATCH/PUT /buildings/1
352
-
353
- # PATCH/PUT /buildings/1.json
354
-
355
- def update
356
-
357
- respond_to do |format|
358
-
359
- if @building.update(building_params)
360
-
361
- format.html { redirect_to @building, notice: 'Building was successfully updated.' }
362
-
363
- format.json { render :show, status: :ok, location: @building }
364
-
365
- else
366
-
367
- format.html { render :edit }
368
-
369
- format.json { render json: @building.errors, status: :unprocessable_entity }
370
-
371
- end
372
-
373
- end
374
-
375
- end
376
-
377
-
378
-
379
- # DELETE /buildings/1
380
-
381
- # DELETE /buildings/1.json
382
-
383
- def destroy
384
-
385
- @building.destroy
386
-
387
- respond_to do |format|
388
-
389
- format.html { redirect_to buildings_url, notice: 'Building was successfully destroyed.' }
390
-
391
- format.json { head :no_content }
392
-
393
- end
394
-
395
- end
396
-
397
-
398
-
399
- private
400
-
401
- # Use callbacks to share common setup or constraints between actions.
402
-
403
- def set_building
404
-
405
- @building = Building.find(params[:id])
406
-
407
- @user = User.find(params[:id])
408
-
409
- end
410
-
411
-
412
-
413
- def correct_user
414
-
415
- building = Building.find(params[:id])
416
-
417
- if current_user.id!=user.id
418
-
419
- redirect_to root_path
420
-
421
- end
422
-
423
- end
424
-
425
- # Never trust parameters from the scary internet, only allow the white list through.
426
-
427
- def building_params
428
-
429
- params.require(:building).permit(:name, :rent, :manage, :deposit, :key_money, :address, :building_image, :building_image_cache, :remove_building_image, :latitude, :longitude)
430
-
431
- end
432
-
433
- end
434
-
435
- ```
436
-
437
- ###試したこと
438
-
439
- 仲間たちと夜通し下記のようなサイト等を調べては試す事を繰り返しましたが、現在まだ解決しておりません。どなたかご教授頂けませんでしょうか。
440
-
441
- ↓belongs_to,has_many関連の記事
442
-
443
- http://keruuweb.com/rails-1対多のモデルを作る/
444
-
445
- http://tkymtk.hatenablog.com/entry/rails-4-three-way-to-write-migration-2014-1
446
-
447
- http://jetglass.hatenablog.jp/entry/2015/04/15/165236
448
-
449
- http://www.stonedot.com/lecture6.html
450
-
451
- http://railsdoc.com/model
452
-
453
- ↓パラメーター関連の記事
454
-
455
- http://railsguides.jp/routing.html
456
-
457
- http://www.rubylife.jp/rails/controller/index6.html
458
-
459
- ###補足情報(言語/FW/ツール等のバージョンなど)
460
-
461
- ruby 2.3.1
462
-
463
- Rails 4.2.6
464
-
465
- mysql Ver 14.14
466
-
467
- osx10.11
468
-
469
- localhost:3000

1

rails ruby models controller has_many belongs_to table column id

2017/01/23 14:31

投稿

nk117
nk117

スコア31

test CHANGED
@@ -1 +1 @@
1
- Rails4 テーブルのカラム情報を共有するにはどうしたら良いしょうか・・・?
1
+ Rails4 別モデルのテーブル情報(idのみ)取得する為のコードの書き方困っております。
test CHANGED
@@ -1,10 +1,10 @@
1
1
  ###前提・実現したいこと
2
2
 
3
- Rails4「建物の詳細ページから合わせができるようなサイト作っています。
3
+ Railsで不動産情報サイトを作っています。各の詳細ページから、お問合わせ(内覧予約)ができるようにしたいのですが、その際に「物件id」と「お問合わせ内容」紐付けしたいと考えています。
4
-
4
+
5
- 現在、問い合わせ情報をrequires,建物情報をbuildingsとして、2つのテーブルを用意しています。
5
+ 物件データを「buildings」、問い合わせ内容requiresとしてDBに保存しています。
6
-
6
+
7
- 問合わせ情報をDB取得する際、buildingsテーブルのidカラムからrequiresテーブルのbuilding_idカラム情報をひっぱりたいですが、良い方法をご存知でしたらご教授頂けませんでしょうか。何卒宜しくお願い申し上げます。
7
+ 内覧予約の際buildingsテーブルのid」を、「Requiresテーブルのbuilding_id」としてDB取得する、良い方法をご存知でしたらご教授頂けませんでしょうか。何卒宜しくお願い申し上げます。
8
8
 
9
9
 
10
10
 
@@ -14,21 +14,447 @@
14
14
 
15
15
  ```
16
16
 
17
- エラーは出ておらず、問い合わテーブルのbuilding_idだけがNULLままです。他のカラム情報は問題なく保存きております。
18
-
19
- ```
20
-
21
-
22
-
23
-
24
-
25
-
17
+ エラーは出ておりまん。buildings左端のカラムが物件idです。
18
+
19
+
20
+
21
+ ```
22
+
23
+ ![参考画像:buildingsのDB](540deae5319c4cc8c21cd53c31f75c00.png)
24
+
25
+ ```
26
+
27
+ requireの右端のカラムのbuilding_idだけ取得できていない状況です。
28
+
29
+ ```
30
+
31
+ ![参考画像:requireのDB](0ef54ded4bed4d29007c6e322c92d45d.png)
32
+
33
+ ###DB
34
+
35
+ ```
36
+
37
+ <db/migrate/create_requires.rb>
38
+
39
+
40
+
41
+ class CreateRequires < ActiveRecord::Migration
42
+
43
+ def change
44
+
45
+ create_table :requires do |t|
46
+
47
+ t.string :name
48
+
49
+ t.string :tel
50
+
51
+ t.string :email
52
+
53
+ t.date :require_date
54
+
55
+ t.time :require_time
56
+
57
+ t.string :gender
58
+
59
+ t.string :age
60
+
61
+ t.string :occupation
62
+
63
+ t.integer :building_id
64
+
65
+ t.timestamps null: false
66
+
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+
74
+
75
+ ```
76
+
77
+ ```
78
+
79
+ <db/migrate/create_buildings.rb>
80
+
81
+
82
+
83
+ class CreateBuildings < ActiveRecord::Migration
84
+
85
+ def change
86
+
87
+ create_table :buildings do |t|
88
+
89
+ t.string :name
90
+
91
+ t.integer :rent
92
+
93
+ t.integer :manage
94
+
95
+ t.integer :deposit
96
+
97
+ t.integer :key_money
98
+
99
+ t.string :address
100
+
101
+ t.string :ward
102
+
103
+ t.integer :user_id
104
+
105
+ t.string :building_image
106
+
107
+ t.decimal :latitude
108
+
109
+ t.decimal :longitude
110
+
111
+ t.timestamps null: false
112
+
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+
120
+
121
+ ```
122
+
123
+ ###Models
124
+
125
+ ```
126
+
127
+ <app/models/require.rb>
128
+
129
+
130
+
131
+ class Require < ActiveRecord::Base
132
+
133
+ belongs_to :building , foreign_key: 'building_id' #←変化なし
134
+
135
+ end
136
+
137
+ ```
138
+
139
+ ```
140
+
141
+ <app/models/buildings.rb>
142
+
143
+
144
+
145
+ class Building < ActiveRecord::Base
146
+
147
+ validates :name,presence: true
148
+
149
+ validates :rent,presence: true
150
+
151
+ validates :manage,presence: true
152
+
153
+ validates :deposit,presence: true
154
+
155
+ validates :key_money,presence: true
156
+
157
+ validates :address,presence: true
158
+
159
+ validates :user_id,presence: true
160
+
161
+ belongs_to :user
162
+
163
+ has_many :requires
164
+
165
+ mount_uploader :building_image, BuildingImageUploader
166
+
167
+ geocoded_by :address
168
+
169
+ after_validation :geocode
170
+
171
+ end
172
+
173
+ ```
174
+
175
+ ###Controller
176
+
177
+ ```
178
+
179
+ <app/requires_controller.rb>
180
+
181
+
182
+
183
+ class RequiresController < InheritedResources::Base
184
+
185
+
186
+
187
+ def create
188
+
189
+ @require = Require.new(require_params)
190
+
191
+
192
+
193
+ respond_to do |format|
194
+
195
+ if @require.save
196
+
197
+ format.html { redirect_to @require, notice: '投稿されました' }
198
+
199
+ format.json { render :show, status: :created, location: @require }
200
+
201
+ else
202
+
203
+ format.html { render :new }
204
+
205
+ format.json { render json: @require.errors, status: :unprocessable_entity }
206
+
207
+ end
208
+
209
+ end
210
+
211
+ end
212
+
213
+
214
+
215
+ private
216
+
217
+
218
+
219
+
220
+
221
+ def require_params
222
+
223
+ params.require(:require).permit(:name, :tel, :email, :require_date, :require_time, :gender, :age, :occupation,)
224
+
225
+ end
226
+
227
+ end
228
+
229
+ ```
230
+
231
+ ```
232
+
233
+ <app/controllers/buildings.controller.rb>
234
+
235
+
236
+
237
+ class BuildingsController < ApplicationController
238
+
239
+ before_action :authenticate_user!, only: [ :new, :edit]
240
+
241
+ before_action :correct_user, only: [ :update]
242
+
243
+ before_action :set_building, only: [ :update, :destroy]
244
+
245
+
246
+
247
+ # GET /buildings
248
+
249
+ # GET /buildings.json
250
+
251
+ def index
252
+
253
+ #@buildings = Building.all
254
+
255
+ @q = Building.search(params[:q])
256
+
257
+ @buildings = @q.result(distinct: true)
258
+
259
+ end
260
+
261
+
262
+
263
+ # GET /buildings/1
264
+
265
+ # GET /buildings/1.json
266
+
267
+ def show
268
+
269
+ end
270
+
271
+
272
+
273
+ # GET /buildings/new
274
+
275
+ def new
276
+
277
+ @building = Building.new
278
+
279
+ end
280
+
281
+
282
+
283
+ # GET /buildings/1/edit
284
+
285
+ def edit
286
+
287
+ @building = Building.find(params[:id])
288
+
289
+ @user = User.find(current_user.id)
290
+
291
+ end
292
+
293
+
294
+
295
+
296
+
297
+ def find
298
+
299
+ @building = Building.find(params[:id])
300
+
301
+ @hash = Gmaps4rails.build_markers(@building) do |building, marker|
302
+
303
+ marker.lat building.latitude
304
+
305
+ marker.lng building.longitude
306
+
307
+ end
308
+
309
+ end
310
+
311
+
312
+
313
+
314
+
315
+ # POST /buildings
316
+
317
+ # POST /buildings.json
318
+
319
+ def create
320
+
321
+ @building = Building.new(building_params)
322
+
323
+ @building.user_id = current_user.id
324
+
325
+
326
+
327
+
328
+
329
+ respond_to do |format|
330
+
331
+ if @building.save
332
+
333
+ format.html { redirect_to @building, notice: '投稿されました' }
334
+
335
+ format.json { render :show, status: :created, location: @building }
336
+
337
+ else
338
+
339
+ format.html { render :new }
340
+
341
+ format.json { render json: @building.errors, status: :unprocessable_entity }
342
+
343
+ end
344
+
345
+ end
346
+
347
+ end
348
+
349
+
350
+
351
+ # PATCH/PUT /buildings/1
352
+
353
+ # PATCH/PUT /buildings/1.json
354
+
355
+ def update
356
+
357
+ respond_to do |format|
358
+
359
+ if @building.update(building_params)
360
+
361
+ format.html { redirect_to @building, notice: 'Building was successfully updated.' }
362
+
363
+ format.json { render :show, status: :ok, location: @building }
364
+
365
+ else
366
+
367
+ format.html { render :edit }
368
+
369
+ format.json { render json: @building.errors, status: :unprocessable_entity }
370
+
371
+ end
372
+
373
+ end
374
+
375
+ end
376
+
377
+
378
+
379
+ # DELETE /buildings/1
380
+
381
+ # DELETE /buildings/1.json
382
+
383
+ def destroy
384
+
385
+ @building.destroy
386
+
387
+ respond_to do |format|
388
+
389
+ format.html { redirect_to buildings_url, notice: 'Building was successfully destroyed.' }
390
+
391
+ format.json { head :no_content }
392
+
393
+ end
394
+
395
+ end
396
+
397
+
398
+
399
+ private
400
+
401
+ # Use callbacks to share common setup or constraints between actions.
402
+
403
+ def set_building
404
+
405
+ @building = Building.find(params[:id])
406
+
407
+ @user = User.find(params[:id])
408
+
409
+ end
410
+
411
+
412
+
413
+ def correct_user
414
+
415
+ building = Building.find(params[:id])
416
+
417
+ if current_user.id!=user.id
418
+
419
+ redirect_to root_path
420
+
421
+ end
422
+
423
+ end
424
+
425
+ # Never trust parameters from the scary internet, only allow the white list through.
426
+
427
+ def building_params
428
+
429
+ params.require(:building).permit(:name, :rent, :manage, :deposit, :key_money, :address, :building_image, :building_image_cache, :remove_building_image, :latitude, :longitude)
430
+
431
+ end
432
+
433
+ end
434
+
435
+ ```
26
436
 
27
437
  ###試したこと
28
438
 
29
- ネッや本使って調べましたが、解決策を見つる事ができませんでし何卒お力添え下さい。
439
+ 仲間たちと夜通し下記のようなサイを調べては試す事を繰り返しましたが、現在まだ解決しておりません。どなたかご教授頂けませんでしょうか
440
+
30
-
441
+ ↓belongs_to,has_many関連の記事
442
+
31
-
443
+ http://keruuweb.com/rails-1対多のモデルを作る/
444
+
445
+ http://tkymtk.hatenablog.com/entry/rails-4-three-way-to-write-migration-2014-1
446
+
447
+ http://jetglass.hatenablog.jp/entry/2015/04/15/165236
448
+
449
+ http://www.stonedot.com/lecture6.html
450
+
451
+ http://railsdoc.com/model
452
+
453
+ ↓パラメーター関連の記事
454
+
455
+ http://railsguides.jp/routing.html
456
+
457
+ http://www.rubylife.jp/rails/controller/index6.html
32
458
 
33
459
  ###補足情報(言語/FW/ツール等のバージョンなど)
34
460