0
0

More than 1 year has passed since last update.

belongs_toのバリデーションについて

Posted at

以下のような中間テーブルを作成した。

tagging.rb
class Tagging < ApplicationRecord
  belongs_to :post
  belongs_to :programming_language
end
schema.rb
create_table "taggings", force: :cascade do |t|
    t.bigint "post_id"
    t.bigint "programming_language_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["post_id"], name: "index_taggings_on_post_id"
    t.index ["programming_language_id"], name: "index_taggings_on_programming_language_id"
end

add_foreign_key "taggings", "posts"
add_foreign_key "taggings", "programming_languages"

このテーブルに対するバリデーションテストを作成している時、現在何もバリデーションを実行するコードを書いていないのにバリデーションが発生したので原因を探った。

belongs_to単体のデフォルト設定について
今回の場合のように単体で用いた場合外部キーカラムに値を設定していなければバリデーションが実行される。

optional: true
このoptionを渡すことによって外部キーカラムに値が入っていなかったり関連先に存在しないidを設定してもバリデーションが発生しない。

required: false
optional: trueと同様の機能を果たすオプションとしてrequired: falseが存在する。
これは、Rails5以前ではデフォルトでrequired: falseになっていたもので当時は外部キーに対するバリデーションを行うためにこのオプションをtrueにしなければならなかった。
しかし、dhh曰くこの挙動はおかしいのでデフォルトでtrueにしようとなったが、デフォルトでtrueとなるオプションはダメなんじゃないの?ということでoptionalという代替オプションが誕生したっぽい
これがその時のissue?

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