Pavel Babin Pavel Babin - 3 months ago 40
Ruby Question

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)

Answer

Paranoid gem has built-in scope to access deleted items: with_deleted.

Mailing.with_deleted.joins(:fields).where(id: mailing_id)