28
23

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】valid?とinvalid?メソッド

Last updated at Posted at 2019-11-12

valid?とinvalid?

valid?

バリデーションが実行された結果
エラーが無い場合trueを返し,エラーが発生した場合falseを返す

micropost.rb
class Micropost < ApplicationRecord
  validates :content, presence: true, length: { maximum: 140 }
end
micropost_test.rb
  test "content should be at most 140 characters" do
    @micropost.content = "a" * 141 #=> false
    assert_not @micropost.valid?
  end

invalid?

valid?とは逆の結果を返す
エラーが発生した場合true,エラーが無い場合falseを返す

参考

[Active Record バリデーション - Rails ガイド] (https://railsguides.jp/active_record_validations.html#valid-questionmark%E3%81%A8invalid-questionmark)

[13.1.2 Micropostのバリデーション - Railsチュートリアル] (https://railstutorial.jp/chapters/user_microposts?version=5.1#sec-micropost_validations)

28
23
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
28
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?