質問編集履歴

1

eventとevent_tagのmodel追記しました

2020/11/27 09:19

投稿

souda-takeru
souda-takeru

スコア4

test CHANGED
File without changes
test CHANGED
@@ -224,6 +224,92 @@
224
224
 
225
225
  end
226
226
 
227
+ ```_event_model
228
+
227
- ```_
229
+ ```ruby
230
+
231
+ class Event < ApplicationRecord
232
+
233
+ extend ActiveHash::Associations::ActiveRecordExtensions
234
+
235
+ belongs_to :user
236
+
237
+ has_many_attached :images
238
+
239
+ belongs_to :facility
240
+
241
+ belongs_to :scale
242
+
243
+ belongs_to :category
244
+
245
+ has_many :event_tag_relations, dependent: :destroy
246
+
247
+ has_many :tags, through: :event_tag_relations, dependent: :destroy
248
+
249
+ end
250
+
251
+ ```event_tag.model
252
+
253
+ ```ruby
254
+
255
+ class EventsTag
256
+
257
+
258
+
259
+ include ActiveModel::Model
260
+
261
+ attr_accessor :name, :explanation, :facility_id, :scale_id, :category_id, :volunteer, :tagname, :user_id
262
+
263
+
264
+
265
+
266
+
267
+ with_options presence: true do
268
+
269
+ validates :name
270
+
271
+ validates :explanation
272
+
273
+ validates :facility_id
274
+
275
+ validates :scale_id
276
+
277
+ validates :category_id
278
+
279
+ end
280
+
281
+ with_options numericality: { other_than: 1 } do
282
+
283
+ validates :facility_id
284
+
285
+ validates :scale_id
286
+
287
+ validates :category_id
288
+
289
+ end
290
+
291
+
292
+
293
+
294
+
295
+ def save
296
+
297
+ event = Event.create(name: name, explanation: explanation, facility_id: facility_id, scale_id: scale_id, category_id: category_id, volunteer: volunteer,user_id: user_id)
298
+
299
+ tag = Tag.where(tagname: tagname).first_or_initialize
300
+
301
+ tag.save
302
+
303
+
304
+
305
+ EventTagRelation.create(event_id: event.id, tag_id: tag.id)
306
+
307
+ end
308
+
309
+
310
+
311
+ end
228
312
 
229
313
  ```
314
+
315
+ ```