質問編集履歴

1

モデルとマイグレーションを追記しました!

2020/12/01 11:08

投稿

Randy_Tozuka
Randy_Tozuka

スコア19

test CHANGED
File without changes
test CHANGED
@@ -171,3 +171,83 @@
171
171
 
172
172
 
173
173
  ```
174
+
175
+ Modelは下記です。
176
+
177
+ ```ruby
178
+
179
+ class Booking < ApplicationRecord
180
+
181
+
182
+
183
+ belongs_to :user
184
+
185
+ validates :user_id, presence: true
186
+
187
+ validates :date, presence: true
188
+
189
+ validates :slot, presence: true
190
+
191
+ # validate :slot_limit
192
+
193
+ default_scope -> {order(date: :asc)}
194
+
195
+
196
+
197
+ # def slot_limit
198
+
199
+ # binding.pry
200
+
201
+ # @booking_date = params[:booking][:date]
202
+
203
+ # @booking_slot = params[:booking][:slot]
204
+
205
+ # @bookings = Booking.where(date:@booking_date).where(slot:@booking_slot)
206
+
207
+ # if @bookings.count > 1
208
+
209
+ # errors.add(:base, 'That slot is fully occupied! Please try other slot.')
210
+
211
+ # end
212
+
213
+ #
214
+
215
+ # end
216
+
217
+
218
+
219
+ end
220
+
221
+ ```
222
+
223
+ 一番最新のmigrationは下記です。
224
+
225
+ ```ruby
226
+
227
+ class CreateBookings < ActiveRecord::Migration[6.0]
228
+
229
+ def change
230
+
231
+ create_table :bookings do |t|
232
+
233
+ t.datetime :date
234
+
235
+ t.string :slot
236
+
237
+ t.references :user, null: false, foreign_key: true
238
+
239
+
240
+
241
+ t.timestamps
242
+
243
+ end
244
+
245
+ #add_index :bookings, [:user_id]
246
+
247
+ end
248
+
249
+ end
250
+
251
+
252
+
253
+ ```