偏执狂 Gem - 加入已删除的项目
Paranoia Gem - joins with deleted items
我正在使用 Paranoia gem,现在正在努力解决这个问题。我需要加入 has_many
个已删除的项目,但它 return 没有被删除而已。我的模特:
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
查询我是 运行 应该 return mailings
删除的项目:
Field.joins(:mailings).where('mailings.id = ?', mailing_id)
paranoid
gem 设置默认范围,仅包括查询中的未删除项。解决方法是:
Field.joins(:mailings).where('mailings.id = ? AND mailings.deleted_at IS NOT NULL', mailing_id)
您可以这样删除查询中的范围:
Field.joins(:mailings).where("mailings.deleted_at != :deleted_status OR mailings.deleted_at = :deleted_status", deleted_status: nil).where(mailings: { id: mailing_id })
或者因为您正在尝试获取似乎是多对多关系的 Field
,所以我更愿意这样反转查询:
Mailing.unscoped.joins(:fields).find(mailing_id).fields
如果我能提供帮助,请告诉我。
偏执狂 gem 具有访问已删除项目的内置范围:with_deleted。
Mailing.with_deleted.joins(:fields).where(id: mailing_id)
到目前为止我找到的唯一可行的解决方案是手动指定 JOIN:
Field.joins('INNER JOIN "mailings" ON "mailings"."id" = "fields"."mailing_id"')
.where('mailings.id = ?', mailing_id)
我参加派对可能有点晚了,但如果有人偶然发现这个问题,这会有所帮助。
您可以在父项和子项之间创建一个额外的关联,以包括已删除的记录。
在所问问题的上下文中,
class Field < ActiveRecord::Base
has_many :mailing_fields, dependent: :destroy
has_many :mailing_fields_with_deleted, -> { with_deleted }
has_many :mailings, through: :mailing_fields
has_many :mailings_with_deleted, through: :mailing_fields_with_deleted
end
然后使用这个关系:
Field.joins(:mailings_with_deleted).where('mailings.id = ?', mailing_id)
我正在使用 Paranoia gem,现在正在努力解决这个问题。我需要加入 has_many
个已删除的项目,但它 return 没有被删除而已。我的模特:
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
查询我是 运行 应该 return mailings
删除的项目:
Field.joins(:mailings).where('mailings.id = ?', mailing_id)
paranoid
gem 设置默认范围,仅包括查询中的未删除项。解决方法是:
Field.joins(:mailings).where('mailings.id = ? AND mailings.deleted_at IS NOT NULL', mailing_id)
您可以这样删除查询中的范围:
Field.joins(:mailings).where("mailings.deleted_at != :deleted_status OR mailings.deleted_at = :deleted_status", deleted_status: nil).where(mailings: { id: mailing_id })
或者因为您正在尝试获取似乎是多对多关系的 Field
,所以我更愿意这样反转查询:
Mailing.unscoped.joins(:fields).find(mailing_id).fields
如果我能提供帮助,请告诉我。
偏执狂 gem 具有访问已删除项目的内置范围:with_deleted。
Mailing.with_deleted.joins(:fields).where(id: mailing_id)
到目前为止我找到的唯一可行的解决方案是手动指定 JOIN:
Field.joins('INNER JOIN "mailings" ON "mailings"."id" = "fields"."mailing_id"')
.where('mailings.id = ?', mailing_id)
我参加派对可能有点晚了,但如果有人偶然发现这个问题,这会有所帮助。
您可以在父项和子项之间创建一个额外的关联,以包括已删除的记录。
在所问问题的上下文中,
class Field < ActiveRecord::Base
has_many :mailing_fields, dependent: :destroy
has_many :mailing_fields_with_deleted, -> { with_deleted }
has_many :mailings, through: :mailing_fields
has_many :mailings_with_deleted, through: :mailing_fields_with_deleted
end
然后使用这个关系:
Field.joins(:mailings_with_deleted).where('mailings.id = ?', mailing_id)