質問編集履歴

1

モデル追加

2019/12/05 14:09

投稿

tenten11055
tenten11055

スコア67

test CHANGED
File without changes
test CHANGED
@@ -233,3 +233,357 @@
233
233
 
234
234
 
235
235
  ※ストロングパラメーターの`su_account_id`はあってもなくても同じ結果でした。
236
+
237
+
238
+
239
+
240
+
241
+ モデルを追記しました。
242
+
243
+ リレーションが怪しい気はしています。。
244
+
245
+
246
+
247
+ ```model
248
+
249
+ class SuAccount < ApplicationRecord
250
+
251
+ # 論理削除使用
252
+
253
+ acts_as_paranoid
254
+
255
+
256
+
257
+ ##
258
+
259
+ # relations
260
+
261
+ ##
262
+
263
+
264
+
265
+ has_one :su_company, class_name: 'SuCompanyInfo'
266
+
267
+ has_one :user, class_name: 'User'
268
+
269
+
270
+
271
+ accepts_nested_attributes_for :su_company, allow_destroy: true
272
+
273
+ accepts_nested_attributes_for :user, allow_destroy: true
274
+
275
+
276
+
277
+ ##
278
+
279
+ # validates
280
+
281
+ ##
282
+
283
+
284
+
285
+ # 部署名
286
+
287
+ validates :group_name,
288
+
289
+ presence: true,
290
+
291
+ length: { maximum: 100 }
292
+
293
+
294
+
295
+ # URL形式チェック
296
+
297
+ if Rails.env == 'production'
298
+
299
+ validates :image_url, format: /\A#{URI::regexp(%w(http https))}\z/
300
+
301
+ end
302
+
303
+ end
304
+
305
+
306
+
307
+ ```
308
+
309
+ ```
310
+
311
+ class SuCompanyInfo < ApplicationRecord
312
+
313
+ # 論理削除用
314
+
315
+ acts_as_paranoid
316
+
317
+
318
+
319
+ ##
320
+
321
+ # relations
322
+
323
+ ##
324
+
325
+
326
+
327
+ belongs_to :su_account, optional: true
328
+
329
+
330
+
331
+ ##
332
+
333
+ # validates
334
+
335
+ ##
336
+
337
+
338
+
339
+ # 企業名
340
+
341
+ validates :company_name,
342
+
343
+ presence: true,
344
+
345
+ length: { maximum: 100 }
346
+
347
+
348
+
349
+ # 企業HP
350
+
351
+ validates :company_hp,
352
+
353
+ length: { maximum: 2048 },
354
+
355
+ format: /\A#{URI::regexp(%w(http https))}\z/
356
+
357
+
358
+
359
+ # 設立年月日
360
+
361
+ validates :establishment_on,
362
+
363
+ date: true,
364
+
365
+ reduce: true
366
+
367
+
368
+
369
+ validates :establishment_year,
370
+
371
+ presence: true, if: -> { establishment_month.presence || establishment_date.presence }
372
+
373
+
374
+
375
+ validates :establishment_month,
376
+
377
+ presence: true, if: -> { establishment_year.presence }
378
+
379
+
380
+
381
+ validates :establishment_date,
382
+
383
+ presence: true, if: -> { establishment_year.presence }
384
+
385
+
386
+
387
+ # 代表者
388
+
389
+ validates :representative,
390
+
391
+ length: { maximum: 50 }
392
+
393
+
394
+
395
+ # 事業内容
396
+
397
+ validates :business_description,
398
+
399
+ length: { maximum: 500 }
400
+
401
+
402
+
403
+ # 資本金
404
+
405
+ validates :capital,
406
+
407
+ length: { maximum: 20 },
408
+
409
+ numericality: {
410
+
411
+ only_integer: true,
412
+
413
+ allow_blank: true
414
+
415
+ }
416
+
417
+
418
+
419
+ # 通貨単位
420
+
421
+ validates :capital_unit,
422
+
423
+ presence: true, if: -> { capital.presence }
424
+
425
+
426
+
427
+ # 所在地
428
+
429
+ validates :address, length: { maximum: 200 }
430
+
431
+
432
+
433
+
434
+
435
+
436
+
437
+ ##
438
+
439
+ # enums
440
+
441
+ ##
442
+
443
+
444
+
445
+ enum capital_unit: {
446
+
447
+ us_dollar: 1,
448
+
449
+ yen: 2,
450
+
451
+ yuan: 3
452
+
453
+ }
454
+
455
+
456
+
457
+
458
+
459
+ attribute :establishment_year
460
+
461
+ attribute :establishment_month
462
+
463
+ attribute :establishment_date
464
+
465
+
466
+
467
+ end
468
+
469
+
470
+
471
+ ```
472
+
473
+
474
+
475
+ ```model
476
+
477
+ class User < ApplicationRecord
478
+
479
+ # 論理削除使用
480
+
481
+ acts_as_paranoid
482
+
483
+
484
+
485
+ devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :lockable, :password_expirable, :secure_validatable, :password_archivable, :expirable
486
+
487
+ include DeviseTokenAuth::Concerns::User
488
+
489
+
490
+
491
+ ##
492
+
493
+ # relations
494
+
495
+ ##
496
+
497
+
498
+
499
+ belongs_to :su_account, optional: true
500
+
501
+
502
+
503
+ ##
504
+
505
+ # enums
506
+
507
+ ##
508
+
509
+
510
+
511
+ enum account_type: { DTVS: "01", DTVSCM: "02", Company: "11", CompanyPIC: "12", SUUser: "21" }, _prefix: true
512
+
513
+
514
+
515
+ ##
516
+
517
+ # validates
518
+
519
+ ##
520
+
521
+
522
+
523
+ validates :account_type,
524
+
525
+ presence: true
526
+
527
+
528
+
529
+ validates :password,
530
+
531
+ password_complexity: true,
532
+
533
+ if: :password_required?
534
+
535
+
536
+
537
+ # 姓
538
+
539
+ validates :last_name,
540
+
541
+ presence: true,
542
+
543
+ length: { maximum: 20 }
544
+
545
+
546
+
547
+ # 名
548
+
549
+ validates :first_name,
550
+
551
+ presence: true,
552
+
553
+ length: { maximum: 20 }
554
+
555
+
556
+
557
+ # メールアドレス
558
+
559
+ validates :email,
560
+
561
+ presence: true,
562
+
563
+ length: { maximum: 256 },
564
+
565
+ email: true,
566
+
567
+ uniqueness: true, on: :create
568
+
569
+
570
+
571
+
572
+
573
+ # 電話番号
574
+
575
+ validates :tel,
576
+
577
+ allow_blank: true,
578
+
579
+ length: { maximum: 25 },
580
+
581
+ format: { with: /(\d|-|+).*/ }
582
+
583
+
584
+
585
+ end
586
+
587
+
588
+
589
+ ```