assessment 3 Flashcards

1
Q

In a belongs_to association, where is the foreign key?

A

In the model in which we define the belongs_to association.

(This model).

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

In a has_many association, where is the foreign key?

A

In the model we assign via class_name

The other model

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

In a has_many/through association, where does the :through association live?

A

In the model in which we define this has_many association.

(This model).

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

Let’s say the foreign_key is stored in this model AND the association is singular.

Do we use a belongs_to or a has_one association?

A

belongs_to

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

Let’s say the foreign_key is stored in a different model AND the association is singular.

Do we use a belongs_to or a has_one association?

A

has_one

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

How do you set up a belongs_to/through association?

A

You don’t.

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

How do you set up a conditional belongs_to association?

A

belongs_to :other_model,
foreign_key: :other_model_id,
optional: true

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

If you have a failing belongs_to/through association, what are 3 things to check?

A
  1. The :through association must already be set up on this model.
  2. The :source association must be set up on the :through model
  3. The :source association must have the correct singular/plural status. Check the spec to see how it is being called.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What SQL keyword do you use to filter by aggregate functions such as COUNT, MAX, AVERGE, etc?

A

HAVING

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

What comes first in a SQL query: WHERE or HAVING?

A

WHERE

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

How do you decide which column to GROUP BY?

A

Look at the desired result and backtrack accordingly.

If we want actors with 20 roles, we want to COUNT a column guaranteed to be unique per table entry, so either actors.id or castings.id.

Which one of these columns will let us find actors?

actors.id

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

When doing a self-join, how do you differentiate between tables?

A

alias each table using AS

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

How do you turn on column headers within sqlite3?

A

.headers ON

.mode column

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