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)
Paranoid gem has built-in scope to access deleted items: with_deleted. Mailing.with_deleted.joins (:fields).where (id: mailing_id) This is insufficient if the parent MailingField object to the Field object is also deleted.
Paranoia does this by setting a deleted_at field to the current time when you destroy a record, and hides it by scoping all queries on your model to only include records which do not have a deleted_at field. If you wish to actually destroy an object you may call really_destroy!.
When your app is using Paranoia, calling destroy on an ActiveRecord object doesn't actually destroy the database record, but just hides it. Paranoia does this by setting a deleted_at field to the current time when you destroy a record, and hides it by scoping all queries on your model to only include records which do not have a deleted_at field.
Paranoia is a re-implementation of acts_as_paranoid for Rails 3/4/5, using much, much, much less code. When your app is using Paranoia, calling destroy on an ActiveRecord object doesn't actually destroy the database record, but just hides it.
The only working solution I've found so far is to manually specify the JOIN:
Field.joins('INNER JOIN "mailings" ON "mailings"."id" = "fields"."mailing_id"')
.where('mailings.id = ?', mailing_id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With