1
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?

【Rails】登録時にバリデーションに引っかかった時に、メッセージが2つ出てくる時に確認すべきところ

Posted at

どうもこんにちは。

今回は小さなことを書きます。

Railsのバリデーションの設定方法

Railsアプリケーションで、バリデーションを設定するとき、以下のように設定をすると思います。

app/models/user.rb
class User < ApplicationRecord
    # ...省略...

    validates :department_id, presence: true

    # ...省略...
end

アソシエーションがある場合

しかし、アソシエーションがある場合、上記のvalidatesを記述してしまうとエラーメッセージが表示されてしまいます。

仮に、departmentsテーブルとusersテーブルに1対多のリレーションが存在する場合、以下のように記述をすると思います。

app/models/user.rb
class User < ApplicationRecord
    # アソシエーションを記述
    belongs_to :department

    # バリデーション
    validates :department_id, presence: true

    # ...省略...
end

このように記載をしてしまうと、以下のような二つのエラーメッセージが表示されます。

department が入力されていません
department_id が入力されていません

エラーメッセージが2つ表示される原因

app/models/user.rbに記述したbelongs_to :userは、デフォルトで関連の存在をバリデートします。

department が入力されていません

対処方法

その1: validates行を削除する

以下のように、validates行をコメントアウトするなり削除するなりすることで、表示されるバリデーションメッセージを1つにすることができます。

app/models/user.rb
class User < ApplicationRecord
    # アソシエーションを記述
    belongs_to :department

    # バリデーション
    # validates :department_id, presence: true

    # ...省略...
end
department が入力されていません

その2: optional: true を指定する

以下のように、belongs_tooptional: trueを指定することで、バリデートを無効にすることができます。

app/models/user.rb
class User < ApplicationRecord
    # アソシエーションを記述
    belongs_to :department, optional: true

    # バリデーション
    validates :department_id, presence: true

    # ...省略...
end

この場合は、validatesのバリデーションが動作するので、以下のエラーメッセージが表示されます。

department_id が入力されていません

アソシエーション対象の外部キーカラムが任意項目である場合

belongs_toでアソシエーション定義したモデルがそもそも任意項目である場合も、optional: trueを指定する必要があります。これを指定しないと、nullが許容されません。

1
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
1
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?