Models Flashcards
1
Q
How can you validate the presence of an attribute for a model?
A
validates :name, presence: true
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
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 }
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