LoginSignup
25
22

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 ガイド

13.1.2 Micropostのバリデーション - Railsチュートリアル

25
22
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
25
22