0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【コア機能】UI再作成、Validation設定

Last updated at Posted at 2025-11-05

目次

  • 1.UI再作成
  • 2.中間テーブル追加
  • 3.Validation追加(9件)

1.UI再作成

丸々2週間UIの作成しなおし。
理由;

  • 色彩的にIT慣れしていない人にはとっつきにくいデザインである。
  • 全体的にUIに統一感がない。

という事で、RailsとMySQLを導入したこのタイミングでUIを再作成する事になりました。

以下、完成版。
Figma

Point

  • 会社のイメージカラーを背景にしました。
  • 縦横のラインが揃うように整えました。
  • パーツkitを作成し、その中のパーツを使用して組み立てました。

2.中間テーブル追加

2025/10/30 week3の木曜日。UI再作成から帰還🚀。。。

問題点
現在は、
CoursesテーブルDestinationsテーブルの間につながりがなく、どの配達先がどのコースに紐づいているかの定義がなされていないことに気づいた。

改定案は2つ
1、Destinationsテーブルにcourse_idカラムを作成し、直接対応させる。
2、DestinationsとCoursesの中間テーブルCourse_Destinationsテーブルを作成する。

結論
2、の中間テーブル作成を採用
理由:
中間テーブルにしておいた方が、後からの修正が楽であるため。
1の方法を取ってしまうと、データベースを触る以外の修正方法がなく、再現性が低い。

テーブル作成手順

手順1 model作成
rails g model CourseDestination course:references destination:references
を実行。

実行結果

class CreateCourseDestinations < ActiveRecord::Migration[8.0]
  def change
    create_table :course_destinations do |t|
      t.references :course, null: false, foreign_key: true
      t.references :destination, null: false, foreign_key: true

      t.timestamps
    end
  end
end

Point
:referenecesをつけることで、他テーブルカラムへの参照(foreign_key)カラムを自動で生成させる。

手順2 migrateする

rails db:migrateを実行

== 20251030132153 CreateCourseDestinations: migrating =========================
-- create_table(:course_destinations)
   -> 0.0705s
== 20251030132153 CreateCourseDestinations: migrated (0.0705s) ================

3.Validation追加(9件)

MySQLにテーブルを作成&DBが登録でき、
全てのテーブルのmodelを作成できたので、
Valitationを追加していきます。

Point
Validationを追加しながら、belongs_to,has_manyの整合性も確認します。

1、employee.rb

class Employee < ApplicationRecord
  has_many :deliveries
  has_many :driver_assignments

  validates :employee_no, presence: true, uniqueness: true
  validates :last_name_ja, :first_name_ja, :last_name_en, :first_name_en, presence: true
  validates :hired_on, presence: true
end

2、course.rb

class Course < ApplicationRecord
  belongs_to :vehicle_type
  has_many :driver_assignments
  has_many :course_destinations
  has_many :destinations, through: :course_destinations
  has_many :deliveries

  validates :name, presence: true, uniqueness: true
  validates :vehicle_type_id, presence: true
end

3、vehicle_type.rb

class VehicleType < ApplicationRecord
  has_many :courses

  validates :name, presence: true, uniqueness: true
end

4、destination.rb

class Destination < ApplicationRecord
  has_many :delivery_stops
  has_many :course_destinations
  has_many :courses, through: :course_destinations

  validates :name, :address, presence: true
  validates :address, uniqueness: true
end

5、delivery.rb

class Delivery < ApplicationRecord
  belongs_to :employee
  belongs_to :course
  has_many :delivery_stops
  has_one :score_snapshot

  validates :employee_id, :course_id, :service_date, :started_at, :finished_at, :odo_start_km, :odo_end_km, presence: true
  validates :odo_start_km, :odo_end_km, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
end

numericality:
→値の検証で使用されるオプション
↑+↓
greater_than_or_equal_to: 0
→入力される数値が必ずマイナスにならない様に制約。
||
numericality:{ only_integer: true, greater_than_or_equal_to: 0 } end
今後セットでよく出てくる。

6、delivery_stop.rb

class DeliveryStop < ApplicationRecord
 belongs_to :delivery
 belongs_to :destination

 validates :stop_no, presence: true
 validates :packages_count, :pieces_count, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
 validates :completed_at, presence: true
end

7、driver_assignment.rb

class DriverAssignment < ApplicationRecord
  belongs_to :employee
  belongs_to :course

  validates :employee_id, :course_id, :effective_from, presence: true
  validate :effective_to_after_effective_from

  private

  def effective_to_after_effective_from
    return if effective_to.blank?
    errors.add(:effective_to, "は開始日以降の日付にしてください") if effective_to < effective_from
  end
end

return if(早期リターン)
原型は

if effective_to.blank?
    return
end

private
+
validate :effective_to_after_effective_from
→カスタムバリデーションを実装。
deiver_assignment.rbでしか呼び出されない内部処理。

8、score_snapshot.rb

 class ScoreSnapshot < ApplicationRecord
  belongs_to :delivery

  validates :work_score, :density_score, :total_score, presence: true
  validates :work_score, :density_score, :total_score, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
end

9、course_destination.rb

class CourseDestination < ApplicationRecord
  belongs_to :course
  belongs_to :destination

  validates :course_id, :destination_id, presence: true
  validates :destination_id, uniqueness: { scope: :course_id }
end

validates :destination_id, uniqueness: { scope: :course_id }
→同じコースに同一住所を登録しない様にする。

4.今後の流れ

・対応するコントローラーとアクションを用意
・対応するViewを用意する。
・テスト

Test

種類 内容 補足
Modelテスト バリデーション・関連チェック RSpecのshoulda-matchersが便利
Controllerテスト アクションが成功するか get :index, post :create など
Systemテスト ブラウザ操作を自動再現 Capybaraで「入力→登録→表示」確認

私のオリジナルプロダクト「Logi-Balance]に関して、
要件定義書や画面遷移図等を以下のGitHub上で公開しております。
GitHub

注意
私はプログラミング学習歴1年未満の初学者です。
この記事は自身の備忘録用として掲載しております。
間違いがあるかと思いますので、今後の学習のためにコメントにご指摘を頂けますと幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?