Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete all on has many relationship #9961

Closed
maletor opened this issue Mar 28, 2013 · 6 comments
Closed

Delete all on has many relationship #9961

maletor opened this issue Mar 28, 2013 · 6 comments

Comments

@maletor
Copy link
Contributor

maletor commented Mar 28, 2013

My assumption is that I should be able to delete all ThreadSubscriptions when I delete an Alert, but instead an exception is raised "ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection: Cannot modify association 'Alert#thread_subscriptions' because the source reflection class 'ThreadSubscription' is associated to 'Notification' via :has_many."

If I am able to SELECT from the association what exactly is preventing me to DELETE from them?

https://gist.github.com/maletor/5259494#file-alert-rb-L23

# == Schema Information
# Schema version: 20130325215115
#
# Table name: alerts
#
#  id          :integer          not null, primary key
#  campaign_id :integer          not null
#  headline    :string(255)      not null
#  sponsor     :string(255)      not null
#  body        :text             not null
#  pi          :text             not null
#  created_at  :datetime
#  updated_at  :datetime
#
# Indexes
#
#  index_alerts_on_campaign_id  (campaign_id)
#

class Alert < ActiveRecord::Base
  include Subscribable
  has_many :notifications, as: :subject, dependent: :delete_all
  has_many :thread_subscriptions, through: :notifications, dependent: :delete_all
  validates :campaign_id, :headline, :sponsor, :body, :pi,
    presence: true

  belongs_to :campaign

  def priority
    3
  end

  def expires_at
    2.weeks.from_now
  end
end
# == Schema Information
# Schema version: 20130325215115
#
# Table name: notifications
#
#  id               :integer          not null, primary key
#  user_id :integer          not null
#  campaign_id      :integer          not null
#  subject_id       :integer          not null
#  subject_type     :string(255)      not null
#  priority         :integer          not null
#  expires_at       :datetime         not null
#  reason           :string(255)      not null
#  last_read_at     :datetime
#  unread           :boolean          default(TRUE), not null
#  created_at       :datetime
#  updated_at       :datetime
#
# Indexes
#
#  index_notifications_on_campaign_id                  (campaign_id)
#  index_notifications_on_user_id             (user_id)
#  index_notifications_on_subject_id_and_subject_type  (subject_id,subject_type)
#

class Notification < ActiveRecord::Base
  belongs_to :subject, polymorphic: true
  belongs_to :campaign
  belongs_to :user
  has_many :thread_subscriptions

  validates :user_id, :campaign_id, :subject_id,
    :subject_type, :priority, :expires_at, :reason, presence: true
end
# == Schema Information
# Schema version: 20130325215115
#
# Table name: thread_subscriptions
#
#  id               :integer          not null, primary key
#  notification_id  :integer          not null
#  user_id :integer          not null
#  subscribed       :boolean          default(TRUE), not null
#  ignored          :boolean          default(FALSE), not null
#  reason           :string(255)
#  created_at       :datetime
#  updated_at       :datetime
#
# Indexes
#
#  index_thread_subscriptions_on_user_id  (user_id)
#  index_thread_subscriptions_on_notification_id   (notification_id)
#

class ThreadSubscription < ActiveRecord::Base
  belongs_to :user
  belongs_to :notification
  validates :user_id, :notification_id, presence: true
  validates :subscribed, :ignored, inclusion: { in: [true, false] }
  validates :subscribed, inclusion: { in: [true] }, unless: :ignored?
  validates :subscribed, inclusion: { in: [false] }, if: :ignored?
  validates :user_id, uniqueness: { scope: :notification_id }
end
@divoxx
Copy link
Contributor

divoxx commented Mar 28, 2013

+1

@neerajsingh0101
Copy link

@Crunch09
Copy link
Contributor

i think you have a nested association there

has_many :notifications, as: :subject, dependent: :delete_all
has_many :thread_subscriptions, through: :notifications, dependent: :delete_all

according to the docs:

When using nested association, you will not be able to modify the association because there is not enough information to know what modification to make.

@senny
Copy link
Member

senny commented May 28, 2013

@maletor what is the state of this issue? Can you please provide an executable test-case as @neerajdotname suggested. This will help us to debug the problem.

@nishantmodak
Copy link
Contributor

Not an issue. Should be closed

https://gist.github.com/nishantmodak/8147200 - Gist Test Case for the issue. / @neerajdotname

@maletor If one ends up using delete instead of destroy then its not going to work, as 'delete' would not look at the associations.

@senny
Copy link
Member

senny commented Dec 27, 2013

It's been several months with no feedback. I'm giving this a close.

Thanks @nishantmodak for the notification. 💛

@senny senny closed this as completed Dec 27, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants