teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

誤字

2017/01/23 14:31

投稿

nk117
nk117

スコア31

title CHANGED
File without changes
body CHANGED
@@ -1,235 +1,3 @@
1
- ###前提・実現したいこと
2
- Railsで不動産情報サイトを作っています。各物件の詳細ページから、お問合わせ(内覧予約)ができるようにしたいのですが、その際に「物件id」と「お問合わせ内容」を紐付けしたいと考えています。
1
+ Railsでサイトを作っています。各物件の詳細ページから、お問合わせ(内覧予約)ができるようにしたいのですが、その際に「id」と「お問合わせ内容」を紐付けしたいと考えています。
3
- 物件データを「buildings」、問い合わせ内容を「requires」としてDBに保存しています。
2
+ 物件データを「books」、問い合わせ内容を「requires」としてDBに保存しています。
4
- 内覧予約の際に「buildingsテーブルのid」を、「Requiresテーブルのbuilding_id」としてDBに取得するのに、良い方法をご存知でしたらご教授頂けませんでしょうか。何卒宜しくお願い申し上げます。
3
+ 内覧予約の際に「booksテーブルのid」を、「Requiresテーブルのbooks_id」としてDBに取得するのに、良い方法をご存知でしたらご教授頂けませんでしょうか。何卒宜しくお願い申し上げます。
5
-
6
- ###発生している問題・エラーメッセージ
7
-
8
- ```
9
- エラーは出ておりません。buildingsの左端のカラムが物件idです。
10
-
11
- ```
12
- ![参考画像:buildingsのDB](540deae5319c4cc8c21cd53c31f75c00.png)
13
- ```
14
- requireの右端のカラムのbuilding_idだけ取得できていない状況です。
15
- ```
16
- ![参考画像:requireのDB](0ef54ded4bed4d29007c6e322c92d45d.png)
17
- ###DB
18
- ```
19
- <db/migrate/create_requires.rb>
20
-
21
- class CreateRequires < ActiveRecord::Migration
22
- def change
23
- create_table :requires do |t|
24
- t.string :name
25
- t.string :tel
26
- t.string :email
27
- t.date :require_date
28
- t.time :require_time
29
- t.string :gender
30
- t.string :age
31
- t.string :occupation
32
- t.integer :building_id
33
- t.timestamps null: false
34
- end
35
- end
36
- end
37
-
38
- ```
39
- ```
40
- <db/migrate/create_buildings.rb>
41
-
42
- class CreateBuildings < ActiveRecord::Migration
43
- def change
44
- create_table :buildings do |t|
45
- t.string :name
46
- t.integer :rent
47
- t.integer :manage
48
- t.integer :deposit
49
- t.integer :key_money
50
- t.string :address
51
- t.string :ward
52
- t.integer :user_id
53
- t.string :building_image
54
- t.decimal :latitude
55
- t.decimal :longitude
56
- t.timestamps null: false
57
- end
58
- end
59
- end
60
-
61
- ```
62
- ###Models
63
- ```
64
- <app/models/require.rb>
65
-
66
- class Require < ActiveRecord::Base
67
- belongs_to :building , foreign_key: 'building_id' #←変化なし
68
- end
69
- ```
70
- ```
71
- <app/models/buildings.rb>
72
-
73
- class Building < ActiveRecord::Base
74
- validates :name,presence: true
75
- validates :rent,presence: true
76
- validates :manage,presence: true
77
- validates :deposit,presence: true
78
- validates :key_money,presence: true
79
- validates :address,presence: true
80
- validates :user_id,presence: true
81
- belongs_to :user
82
- has_many :requires
83
- mount_uploader :building_image, BuildingImageUploader
84
- geocoded_by :address
85
- after_validation :geocode
86
- end
87
- ```
88
- ###Controller
89
- ```
90
- <app/requires_controller.rb>
91
-
92
- class RequiresController < InheritedResources::Base
93
-
94
- def create
95
- @require = Require.new(require_params)
96
-
97
- respond_to do |format|
98
- if @require.save
99
- format.html { redirect_to @require, notice: '投稿されました' }
100
- format.json { render :show, status: :created, location: @require }
101
- else
102
- format.html { render :new }
103
- format.json { render json: @require.errors, status: :unprocessable_entity }
104
- end
105
- end
106
- end
107
-
108
- private
109
-
110
-
111
- def require_params
112
- params.require(:require).permit(:name, :tel, :email, :require_date, :require_time, :gender, :age, :occupation,)
113
- end
114
- end
115
- ```
116
- ```
117
- <app/controllers/buildings.controller.rb>
118
-
119
- class BuildingsController < ApplicationController
120
- before_action :authenticate_user!, only: [ :new, :edit]
121
- before_action :correct_user, only: [ :update]
122
- before_action :set_building, only: [ :update, :destroy]
123
-
124
- # GET /buildings
125
- # GET /buildings.json
126
- def index
127
- #@buildings = Building.all
128
- @q = Building.search(params[:q])
129
- @buildings = @q.result(distinct: true)
130
- end
131
-
132
- # GET /buildings/1
133
- # GET /buildings/1.json
134
- def show
135
- end
136
-
137
- # GET /buildings/new
138
- def new
139
- @building = Building.new
140
- end
141
-
142
- # GET /buildings/1/edit
143
- def edit
144
- @building = Building.find(params[:id])
145
- @user = User.find(current_user.id)
146
- end
147
-
148
-
149
- def find
150
- @building = Building.find(params[:id])
151
- @hash = Gmaps4rails.build_markers(@building) do |building, marker|
152
- marker.lat building.latitude
153
- marker.lng building.longitude
154
- end
155
- end
156
-
157
-
158
- # POST /buildings
159
- # POST /buildings.json
160
- def create
161
- @building = Building.new(building_params)
162
- @building.user_id = current_user.id
163
-
164
-
165
- respond_to do |format|
166
- if @building.save
167
- format.html { redirect_to @building, notice: '投稿されました' }
168
- format.json { render :show, status: :created, location: @building }
169
- else
170
- format.html { render :new }
171
- format.json { render json: @building.errors, status: :unprocessable_entity }
172
- end
173
- end
174
- end
175
-
176
- # PATCH/PUT /buildings/1
177
- # PATCH/PUT /buildings/1.json
178
- def update
179
- respond_to do |format|
180
- if @building.update(building_params)
181
- format.html { redirect_to @building, notice: 'Building was successfully updated.' }
182
- format.json { render :show, status: :ok, location: @building }
183
- else
184
- format.html { render :edit }
185
- format.json { render json: @building.errors, status: :unprocessable_entity }
186
- end
187
- end
188
- end
189
-
190
- # DELETE /buildings/1
191
- # DELETE /buildings/1.json
192
- def destroy
193
- @building.destroy
194
- respond_to do |format|
195
- format.html { redirect_to buildings_url, notice: 'Building was successfully destroyed.' }
196
- format.json { head :no_content }
197
- end
198
- end
199
-
200
- private
201
- # Use callbacks to share common setup or constraints between actions.
202
- def set_building
203
- @building = Building.find(params[:id])
204
- @user = User.find(params[:id])
205
- end
206
-
207
- def correct_user
208
- building = Building.find(params[:id])
209
- if current_user.id!=user.id
210
- redirect_to root_path
211
- end
212
- end
213
- # Never trust parameters from the scary internet, only allow the white list through.
214
- def building_params
215
- params.require(:building).permit(:name, :rent, :manage, :deposit, :key_money, :address, :building_image, :building_image_cache, :remove_building_image, :latitude, :longitude)
216
- end
217
- end
218
- ```
219
- ###試したこと
220
- 仲間たちと夜通し下記のようなサイト等を調べては試す事を繰り返しましたが、現在まだ解決しておりません。どなたかご教授頂けませんでしょうか。
221
- ↓belongs_to,has_many関連の記事
222
- http://keruuweb.com/rails-1対多のモデルを作る/
223
- http://tkymtk.hatenablog.com/entry/rails-4-three-way-to-write-migration-2014-1
224
- http://jetglass.hatenablog.jp/entry/2015/04/15/165236
225
- http://www.stonedot.com/lecture6.html
226
- http://railsdoc.com/model
227
- ↓パラメーター関連の記事
228
- http://railsguides.jp/routing.html
229
- http://www.rubylife.jp/rails/controller/index6.html
230
- ###補足情報(言語/FW/ツール等のバージョンなど)
231
- ruby 2.3.1
232
- Rails 4.2.6
233
- mysql Ver 14.14
234
- osx10.11
235
- localhost:3000

1

rails ruby models controller has_many belongs_to table column id

2017/01/23 14:31

投稿

nk117
nk117

スコア31

title CHANGED
@@ -1,1 +1,1 @@
1
- Rails4 テーブルのカラム情報を共有するにはどうしたら良いしょうか・・・?
1
+ Rails4 別モデルのテーブル情報(idのみ)取得する為のコードの書き方困っております。
body CHANGED
@@ -1,19 +1,232 @@
1
1
  ###前提・実現したいこと
2
- Rails4「建物の詳細ページから合わせができるようなサイト作っています。
2
+ Rails不動産情報サイトを作っています。各の詳細ページから、お問合わせ(内覧予約)ができるようにしたいのですが、その際に「物件id」と「お問合わせ内容」紐付けしたいと考えています。
3
- 現在、問い合わせ情報をrequires,建物情報をbuildingsとして、2つのテーブルを用意しています。
3
+ 物件データを「buildings」、問い合わせ内容requiresとしてDBに保存しています。
4
- 問合わせ情報をDBに取得するbuildingsテーブルのidカラムからrequiresテーブルのbuilding_idカラム情報をひっぱりたいので、良い方法をご存知でしたらご教授頂けませんでしょうか。何卒宜しくお願い申し上げます。
4
+ 内覧予約のに「buildingsテーブルのid」を、「Requiresテーブルのbuilding_id」としてDB取得るのに、良い方法をご存知でしたらご教授頂けませんでしょうか。何卒宜しくお願い申し上げます。
5
5
 
6
6
  ###発生している問題・エラーメッセージ
7
7
 
8
8
  ```
9
- エラーは出ておらず、問い合わせテーブルのbuilding_idだけがNULLのまですのカラム情報は問題なく保存きております。
9
+ エラーは出ておせんbuildings左端のカラムが物件idです。
10
+
10
11
  ```
12
+ ![参考画像:buildingsのDB](540deae5319c4cc8c21cd53c31f75c00.png)
13
+ ```
14
+ requireの右端のカラムのbuilding_idだけ取得できていない状況です。
15
+ ```
16
+ ![参考画像:requireのDB](0ef54ded4bed4d29007c6e322c92d45d.png)
17
+ ###DB
18
+ ```
19
+ <db/migrate/create_requires.rb>
11
20
 
21
+ class CreateRequires < ActiveRecord::Migration
22
+ def change
23
+ create_table :requires do |t|
24
+ t.string :name
25
+ t.string :tel
26
+ t.string :email
27
+ t.date :require_date
28
+ t.time :require_time
29
+ t.string :gender
30
+ t.string :age
31
+ t.string :occupation
32
+ t.integer :building_id
33
+ t.timestamps null: false
34
+ end
35
+ end
36
+ end
12
37
 
38
+ ```
39
+ ```
40
+ <db/migrate/create_buildings.rb>
13
41
 
42
+ class CreateBuildings < ActiveRecord::Migration
43
+ def change
44
+ create_table :buildings do |t|
45
+ t.string :name
46
+ t.integer :rent
47
+ t.integer :manage
48
+ t.integer :deposit
49
+ t.integer :key_money
50
+ t.string :address
51
+ t.string :ward
52
+ t.integer :user_id
53
+ t.string :building_image
54
+ t.decimal :latitude
55
+ t.decimal :longitude
56
+ t.timestamps null: false
57
+ end
58
+ end
59
+ end
60
+
61
+ ```
62
+ ###Models
63
+ ```
64
+ <app/models/require.rb>
65
+
66
+ class Require < ActiveRecord::Base
67
+ belongs_to :building , foreign_key: 'building_id' #←変化なし
68
+ end
69
+ ```
70
+ ```
71
+ <app/models/buildings.rb>
72
+
73
+ class Building < ActiveRecord::Base
74
+ validates :name,presence: true
75
+ validates :rent,presence: true
76
+ validates :manage,presence: true
77
+ validates :deposit,presence: true
78
+ validates :key_money,presence: true
79
+ validates :address,presence: true
80
+ validates :user_id,presence: true
81
+ belongs_to :user
82
+ has_many :requires
83
+ mount_uploader :building_image, BuildingImageUploader
84
+ geocoded_by :address
85
+ after_validation :geocode
86
+ end
87
+ ```
88
+ ###Controller
89
+ ```
90
+ <app/requires_controller.rb>
91
+
92
+ class RequiresController < InheritedResources::Base
93
+
94
+ def create
95
+ @require = Require.new(require_params)
96
+
97
+ respond_to do |format|
98
+ if @require.save
99
+ format.html { redirect_to @require, notice: '投稿されました' }
100
+ format.json { render :show, status: :created, location: @require }
101
+ else
102
+ format.html { render :new }
103
+ format.json { render json: @require.errors, status: :unprocessable_entity }
104
+ end
105
+ end
106
+ end
107
+
108
+ private
109
+
110
+
111
+ def require_params
112
+ params.require(:require).permit(:name, :tel, :email, :require_date, :require_time, :gender, :age, :occupation,)
113
+ end
114
+ end
115
+ ```
116
+ ```
117
+ <app/controllers/buildings.controller.rb>
118
+
119
+ class BuildingsController < ApplicationController
120
+ before_action :authenticate_user!, only: [ :new, :edit]
121
+ before_action :correct_user, only: [ :update]
122
+ before_action :set_building, only: [ :update, :destroy]
123
+
124
+ # GET /buildings
125
+ # GET /buildings.json
126
+ def index
127
+ #@buildings = Building.all
128
+ @q = Building.search(params[:q])
129
+ @buildings = @q.result(distinct: true)
130
+ end
131
+
132
+ # GET /buildings/1
133
+ # GET /buildings/1.json
134
+ def show
135
+ end
136
+
137
+ # GET /buildings/new
138
+ def new
139
+ @building = Building.new
140
+ end
141
+
142
+ # GET /buildings/1/edit
143
+ def edit
144
+ @building = Building.find(params[:id])
145
+ @user = User.find(current_user.id)
146
+ end
147
+
148
+
149
+ def find
150
+ @building = Building.find(params[:id])
151
+ @hash = Gmaps4rails.build_markers(@building) do |building, marker|
152
+ marker.lat building.latitude
153
+ marker.lng building.longitude
154
+ end
155
+ end
156
+
157
+
158
+ # POST /buildings
159
+ # POST /buildings.json
160
+ def create
161
+ @building = Building.new(building_params)
162
+ @building.user_id = current_user.id
163
+
164
+
165
+ respond_to do |format|
166
+ if @building.save
167
+ format.html { redirect_to @building, notice: '投稿されました' }
168
+ format.json { render :show, status: :created, location: @building }
169
+ else
170
+ format.html { render :new }
171
+ format.json { render json: @building.errors, status: :unprocessable_entity }
172
+ end
173
+ end
174
+ end
175
+
176
+ # PATCH/PUT /buildings/1
177
+ # PATCH/PUT /buildings/1.json
178
+ def update
179
+ respond_to do |format|
180
+ if @building.update(building_params)
181
+ format.html { redirect_to @building, notice: 'Building was successfully updated.' }
182
+ format.json { render :show, status: :ok, location: @building }
183
+ else
184
+ format.html { render :edit }
185
+ format.json { render json: @building.errors, status: :unprocessable_entity }
186
+ end
187
+ end
188
+ end
189
+
190
+ # DELETE /buildings/1
191
+ # DELETE /buildings/1.json
192
+ def destroy
193
+ @building.destroy
194
+ respond_to do |format|
195
+ format.html { redirect_to buildings_url, notice: 'Building was successfully destroyed.' }
196
+ format.json { head :no_content }
197
+ end
198
+ end
199
+
200
+ private
201
+ # Use callbacks to share common setup or constraints between actions.
202
+ def set_building
203
+ @building = Building.find(params[:id])
204
+ @user = User.find(params[:id])
205
+ end
206
+
207
+ def correct_user
208
+ building = Building.find(params[:id])
209
+ if current_user.id!=user.id
210
+ redirect_to root_path
211
+ end
212
+ end
213
+ # Never trust parameters from the scary internet, only allow the white list through.
214
+ def building_params
215
+ params.require(:building).permit(:name, :rent, :manage, :deposit, :key_money, :address, :building_image, :building_image_cache, :remove_building_image, :latitude, :longitude)
216
+ end
217
+ end
218
+ ```
14
219
  ###試したこと
15
- ネッや本使って調べましたが、解決策を見つる事ができませんでし何卒お力添え下さい。
220
+ 仲間たちと夜通し下記のようなサイを調べては試す事を繰り返しましたが、現在まだ解決しておりません。どなたかご教授頂けませんでしょうか
16
-
221
+ ↓belongs_to,has_many関連の記事
222
+ http://keruuweb.com/rails-1対多のモデルを作る/
223
+ http://tkymtk.hatenablog.com/entry/rails-4-three-way-to-write-migration-2014-1
224
+ http://jetglass.hatenablog.jp/entry/2015/04/15/165236
225
+ http://www.stonedot.com/lecture6.html
226
+ http://railsdoc.com/model
227
+ ↓パラメーター関連の記事
228
+ http://railsguides.jp/routing.html
229
+ http://www.rubylife.jp/rails/controller/index6.html
17
230
  ###補足情報(言語/FW/ツール等のバージョンなど)
18
231
  ruby 2.3.1
19
232
  Rails 4.2.6