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?

More than 3 years have passed since last update.

railsでnull制限、空文字制限をかける

Posted at

この記事の目的

railsでnull制限をかける際にやらないといけないこととその実装内容。

全体

  • dbのカラムにnull制限をかける
  • カラムへのnull制限だけだと空文字を入れれてしまうのでmodelにvalidation

dbのカラムにnull制限をかける

def change
  change_column_null :skill_maps, :skill_id, false
end

null制限をかける場合(falseはnullの許可がfalse)

change_column_null :テーブル名, :カラム名, false

modelにvalidation

カラムへのnull制限だけだと空文字を入れれてしまうのでmodelにvalidation

validates :content, presence: true
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?