ruby-on-railsrubyruby-paranoia

Paranoia Gem - joins with deleted items


I'm using Paranoia gem and now struggling with the problem. I need to joins has_many deleted items, but it returns not deleted only. My models:

class Mailing < ActiveRecord::Base

  acts_as_paranoid

  has_many :mailing_fields
  has_many :fields, through: :mailing_fields

end

class MailingField < ActiveRecord::

  belongs_to :mailing
  belongs_to :field

end

class Field < ActiveRecord::Base

  has_many :mailing_fields, dependent: :destroy
  has_many :mailings, through: :mailing_fields

end

Query I'm running which should return mailings with deleted items:

Field.joins(:mailings).where('mailings.id = ?', mailing_id)

Solution

  • The paranoid gem sets a default scope of only including non-deleted items in queries. The workaround would be:

    Field.joins(:mailings).where('mailings.id = ? AND mailings.deleted_at IS NOT NULL', mailing_id)