ActiveRecord Flashcards

0
Q

Starting with a Student model, how would you join to the schools table and then narrow the results to include only public schools (“category” is “public”)?

A

Student.join(:schools).where(schools: {category: “public”})

OR

Student.join(:schools).where(“schools.category” => “public”)

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

How would you retrieve from the data store a group of students who are between the ages of 7 and 9?

A

Student.where(age: 7..9)
# OR
Student.where(age: [7,8,9])

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

How do you override accessors for models?

A

With the read_attribute and write_attribute methods.

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

Due to the nature of the web, the data input by the user will always be strings. ActiveRecord, however, tries to coerce the data into the specified column’s type. How do you access the original data?

A

model._before_type_cast

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

How can you store complex data in a single text column?

A

Model < ActiveRecord::Base
serialize :complex_data
end

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

How can you force serialized data to be of a specific class?

A

Pass the class name as the second argument to the serialize method:

serialize :attribute, OpenStruct

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

How do you validate that a string matches the pattern /\w+/?

A

validates :attributes, format: { with: /\w+/ }

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

How do you validate that a string is not either “a,” “the,” or “or”?

A

validates :attribute, exclusion: { in: %w(a the or) }

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

How do you validate that a shirt size is either “small,” “medium,” or “large?”

A

validates :size, inclusion: { in: %w(small medium large) }

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

How do you validate that a password at least 6 characters long?

A

validates :password, length: { minimun: 6 }

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

How do you validate that a password is less than 20 characters long?

A

validates :password, length: { maximum: 20 }

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

How do you validate that a password is in between 6 and 20 characters?

A

validates :password, length: { in: 6..20 }

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

How do you validate that a string is exactly 25 characters long?

A

validates :attribute, length: { is: 25 }

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

For length validations, which symbols can you use for error messages?

A

:wrong_length
:too_long
:too_short

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

How do you validate that a string has at least 300 words?

A

validates :attribute, length: {
minimum: 300,
tokenizer: lambda { |str| str.scan(/\w+/) }
}

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

How do you validate that :age is a number?

A

validates :age, numericality: true

16
Q

How do you confirm that :age in an integer

A

validates :age, numericality: { only_integer: true }

17
Q

What are the acceptable constraints for numericality?

A
\:greater_than
\:less_than
\:greater_than_or_equal_to
\:less_than_or_equal_to
\:equal_to
\:even
\:odd
18
Q

How do you confirm that a string is not nil or blank?

A

validates :attribute, presence: true