assessment 3 Flashcards
In a belongs_to association, where is the foreign key?
In the model in which we define the belongs_to association.
(This model).
In a has_many association, where is the foreign key?
In the model we assign via class_name
The other model
In a has_many/through association, where does the :through association live?
In the model in which we define this has_many association.
(This model).
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?
belongs_to
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?
has_one
How do you set up a belongs_to/through association?
You don’t.
How do you set up a conditional belongs_to association?
belongs_to :other_model,
foreign_key: :other_model_id,
optional: true
If you have a failing belongs_to/through association, what are 3 things to check?
- The :through association must already be set up on this model.
- The :source association must be set up on the :through model
- The :source association must have the correct singular/plural status. Check the spec to see how it is being called.
What SQL keyword do you use to filter by aggregate functions such as COUNT, MAX, AVERGE, etc?
HAVING
What comes first in a SQL query: WHERE or HAVING?
WHERE
How do you decide which column to GROUP BY?
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
When doing a self-join, how do you differentiate between tables?
alias each table using AS
How do you turn on column headers within sqlite3?
.headers ON
.mode column