Rails 4: Delete on has_many through association

matthias

Based on one of the examples in the Rails Guides (http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association) I wrote the following code:

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, through: :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient
end

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, through: :appointments

  def vips
    patients.where(appointments: { vip: true })
  end

  def add_vip patient
    appointments.create(patient: patient, vip: true)
  end

  def delete_vip patient
    vips.delete(patient)
  end
end

The problem is that if I have a physician (an instance of Physician) and a patient and then do

physician.add_vip(patient)
physician.delete_vip(patient)

the delete_vip not only deletes the association but the instance itself.

How can I delete only the association?

Rob

The association/record you want to delete is the Appointment not the Patient. You are creating an appointment in add_vip and should be removing an appointment in delete_vip.

# Deletes all appointments for that patient without destroying the patient itself.
def delete_vip patient
  appointments.where(patient: patient, vip: true).delete_all
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails delete has_many through association

From Dev

Rails 4: combining has_many :through association with polymorphic association

From Dev

Rails, How to delete an association in has_many through

From Dev

Rails 4: CanCanCan abilities with has_many :through association

From Dev

Rails 4: routing for has_many :through association

From Dev

Rails 4: Create new records through has_many association

From Dev

Rails 4 - STI has_many :through with polymorphic association

From Dev

Issue with join table and has_many through association in Rails 4

From Dev

rails scope through has_many association

From Dev

Rails has_many :through association

From Dev

rails scope through has_many association

From Dev

Rails has_many through association issue

From Dev

Rails soft delete with has_many association

From Dev

Rails 4: has many through association error

From Dev

Rails 4 has_many through many

From Dev

Rails 4 + Pundit : join model authorization in has_many :through association

From Dev

Rails 4: counter_cache in has_many :through association with dependent: :destroy

From Dev

Rails 4 Could not find the association has_many, through: relationship error

From Dev

How to update has_many :through association with strong parameters in Rails 4

From Dev

rails4 collection select with has_many through association and nested model forms

From Dev

Rails 4 - Cannot modify association because of :has_many association

From Dev

has_many / :through rails4

From Dev

Using has_many through in Rails 4

From Dev

has_many / :through rails4

From Dev

Rails/ActiveRecord has_many through: association on unsaved objects

From Dev

Weird issue has_many through association in updated Rails

From Dev

Rails has_many :through association: save instance into join table

From Dev

Rails: has_many association with self through another table

From Dev

Rails 5 Postgresql has_many through association

Related Related

  1. 1

    Rails delete has_many through association

  2. 2

    Rails 4: combining has_many :through association with polymorphic association

  3. 3

    Rails, How to delete an association in has_many through

  4. 4

    Rails 4: CanCanCan abilities with has_many :through association

  5. 5

    Rails 4: routing for has_many :through association

  6. 6

    Rails 4: Create new records through has_many association

  7. 7

    Rails 4 - STI has_many :through with polymorphic association

  8. 8

    Issue with join table and has_many through association in Rails 4

  9. 9

    rails scope through has_many association

  10. 10

    Rails has_many :through association

  11. 11

    rails scope through has_many association

  12. 12

    Rails has_many through association issue

  13. 13

    Rails soft delete with has_many association

  14. 14

    Rails 4: has many through association error

  15. 15

    Rails 4 has_many through many

  16. 16

    Rails 4 + Pundit : join model authorization in has_many :through association

  17. 17

    Rails 4: counter_cache in has_many :through association with dependent: :destroy

  18. 18

    Rails 4 Could not find the association has_many, through: relationship error

  19. 19

    How to update has_many :through association with strong parameters in Rails 4

  20. 20

    rails4 collection select with has_many through association and nested model forms

  21. 21

    Rails 4 - Cannot modify association because of :has_many association

  22. 22

    has_many / :through rails4

  23. 23

    Using has_many through in Rails 4

  24. 24

    has_many / :through rails4

  25. 25

    Rails/ActiveRecord has_many through: association on unsaved objects

  26. 26

    Weird issue has_many through association in updated Rails

  27. 27

    Rails has_many :through association: save instance into join table

  28. 28

    Rails: has_many association with self through another table

  29. 29

    Rails 5 Postgresql has_many through association

HotTag

Archive