Chapter 7 - Active Record Associations Flashcards

1
Q

Add index

A

add_index :timesheets, :user_id

add_index :expense_reports, :user_id

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

pluck

A

Use pluck as a shortcut to select one or more attributes without loading a bunch of records just to grab the attributes you want.
Pluck returns an array.

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

has_many

A

the has_many association allows you to define a relationship in which one model has many other models that belong to it.

has_many :timesheets

has_many :expense_reports

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

One-To-Many

A

class User

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

where(*conditions)

A

has_many :pending_comments, -> { where( approved: true) }, class_name: ‘Comment’

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

has_and_belongs_to_many

A

I must clear my conscience by stating that

has_and_belongs_to_many is practically obsolete.

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

create_join_table

A

create_join_table :billing_codes, :timesheets

class Timesheet

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

has_many :through

A

The has_many :through association allows you to specify a one-to-many relationship indirectly via an intermediate join table. In fact, you can specify more than one such relationship via the same table, which effectively makes it a replacement for has_and_belongs_to_many. The biggest advantage is that the join table contains full-fledged model objects complete with primary keys and ancillary data.

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

has_many :through implementation

A

class Client

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

has_many :through as aggregating

A

class Grandparent

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

Join models and validations

A

You can add validates_uniqueness_of constraints on the join model to keep duplicate joins from happening.

validates_uniqueness_of :client_id, scope: :timesheet_id

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

One-to-One Relationship

A

One of the most basic relationship types is a one-to-one object relationship. In Active Record we declare a one-to-one relationship using the has_one and belongs_to methods together.

class User

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