Association Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How to add belongs_to association?

A

belongs_to :user

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

How to add has_many association?

A

has_many :dogs

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

How belongs_to works inside?

A
# belongs_to :user
# something like this
define_method :user do
  User.find(self.user_id)
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How has_many works inside?

A
#  has_many :dogs
# something like this

define_method :dogs do
Dog.where(user_id: self.id)
end

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

How to destroy all associations record together with a parent record?

A

has_many :dogs, dependent: :destroy

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

How to not destroy (set parent_id to nil) all associations record together with a parent record?

A

has_many :dogs, dependent: :nullify

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

How to make belong_to association optional?

A

belongs_to :user, optional: true

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

What is polymorphic association? Any examples?

A

“with polymorphic associations, a model can belong to more than one other model, on a single association”.

class Cat < ApplicationRecord
  # can belongs_to only one of them 
  belongs_to :human
  belongs_to :zoo
  belongs_to :restaurant
  belongs_to :streat 

# why do we need to write all? let’s use polymorphic

  belongs_to :owner, polymorphic: true 
  # and this model has field owner_id and owner_type
  # for example owner_id=1 owner_type="Human"
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to create a polymorphic association?

A
class Appointment < ApplicationRecord
  belongs_to :sick_animal, polymorphic: true
end
class Cat < ApplicationRecord
  has_many :appointments, as: :sick_animal
end
class Dog < ApplicationRecord
  has_many :appointments, as: :sick_animal
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to add belongs_to association with another class?

A

belongs_to :user, class_name: ‘Cat’

# Dog.create(user: Cat.last) -> works
# Dog.create(user: User.last) -> doesn't work
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to add belongs_to association with a ‘where’ condition?

A
class Dog < ApplicationRecord
  belongs_to :user, -> { where(status: true) }
end

It doesn’t return a user with a false condition for a dog.

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

How to create a record through has_many clause? ( 2 ways)

A

user has_many dogs

  1. User.last.dogs.create
  2. User.last.dogs &laquo_space;Dog.create
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between delete_all and destroy_all?

A
  • delete_all doesn’t destroy the association, only nullify them
  • destroy_all delete association as well
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to add where condition to has_many association?

A
class User < ApplicationRecord
  has_many :live_dogs, -> { where(alive: true) }, class_name: 'Dog'
  has_many :dead_dogs, -> { where(alive: false) }, class_name: 'Dog'
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the main differences between has_and_belongs_to_many and has_many through?

A
  • has_and_belongs_to_many - implicitly creates only join table for you.
  • has_many through - explicitly creates a join table and a model for you.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to create has_and_belongs_to_many association?

A
class Dog < ApplicationRecord
  has_and_belongs_to_many :users
end
class User < ApplicationRecord
  has_and_belongs_to_many :dogs
end

create a join_table for these two models

17
Q

How to create self-referential many to many associations?

A
1. Create a join table with 2 ids field 
  def change
    create_table :user_join_tables do |t|
      t.column :first_user_id, :integer
      t.column :second_user_id, :integer
    end
  end
2. Set up model
  has_and_belongs_to_many :users,
    join_table: 'user_join_tables',
    foreign_key: 'first_user_id',
    association_foreign_key: 'second_user_id',
    class_name: 'User'
18
Q

How to create has_many through association?

A
class User < ApplicationRecord
  has_many :appointments
  has_many :dogs, through: :appointments
end
class Dog < ApplicationRecord
  has_many :appointments
  has_many :users, through: :appointments
end
class Appointment < ApplicationRecord
  belongs_to :dog
  belongs_to :user
end
19
Q

How to create has_many through with another model?

A
# use source
has_many :alive_dogs, through: :appointments, source: :dog
20
Q

What is the difference between :source and :class_name?

A

They are conceptually the same, just need to be different for different uses.

:source is used (optionally) to define the associated model name when you’re using has_many through; :class_name is used (optionally) in a simple has many relationship. Both are needed only if Rails cannot figure out the class name on its own.