Validation Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How to add validation on presence?

A

validates :name, presence: true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to validate that value is blank?

A

validates :name, absence: true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to check that record is valid? or invalid?

A
  • User.new(name: ‘a’).valid?

- User.new(name: ‘a’).invalid?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to show a list of errors?

A

u = User.new(name: ‘a’)

u. valid?
u. errors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to validate acceptance of the field? ( accept the agreement or not)

A

validates :terms_of_service, acceptance: true

User.new(terms_of_service: 0).valid? # false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to validate record formate with regexp?

A

validates :name, format: { with: /[a-z]/ }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to check that value is included in the given array?

A

validates :name, inclusion: { in: [‘a’, ‘b’] }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to validate length of the record?

A

validates :name, length: { minimum: 10, maximum: 20 }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to validate that value is a number?

A

validates :name, numericality: true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between validates and validate?

A
  • “validate” for custom validation

- “validates” for rails validation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to add a custom validation?

A

validate :custom_validation

  def custom_validation
    errors.add(:name, 'custom_error') if true
  end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to validate that value is unique?

A

validates :name, uniqueness: true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly