Models Flashcards

1
Q

How can you validate the presence of an attribute for a model?

A

validates :name, presence: true

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

How can you add a has many relationship to another model and how about an belongs to?

A

has_many :articles

belongs_to :user

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

How can you validate the value of an attribute, ex: name to equal either ‘Scott’ or ‘Joe’.

A

Create an constant with the array of your wanted values:
VALID_NAMES = [‘Scott’, ‘Joe’]
validates :name, inclusion: { in: VALID_NAMES }

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

How can you validate the uniqueness of an attribute value, ex: email to be unique, only one hottie@test.com

A

validates :email, uniqueness: true

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