Rails 关联 with_deleted 范围不适用于订单

Rails association with_deleted scope doesn't work with order

这是我目前得到的:

class MemberSitePosition < ActiveRecord::Base
  acts_as_paranoid
  belongs_to :member, -> { with_deleted }
  belongs_to :position
  ...
}
class Member < ActiveRecord::Base
  include ::Hierarchical
  acts_as_paranoid
  has_many   :member_site_positions
  ...
}

// Get all member associated to a site with their position
@msps = member_site_positions.includes(:position, { member { profile: :item }}).where(position_id: position_ids, member_id: member_ids)

@msps.each do |msp|
    member = msp.member
   // ... do stuff with member
end

这个查询就像一个魅力,我让我所有的成员都与他们的位置相关联到我的所有站点。

但是当我尝试订购它们时,我得到了

@msps = member_site_positions.includes(:position, { member { profile: :item }}).where(position_id: position_ids, member_id: member_ids)
.order("positions.order") // adding the order

@msps.each do |msp|
    member = msp.member // Member is nil only if I pass the order (and member is deleted)
end

当我尝试访问它们时,删除的成员现在为零。

我发现当我添加订单时,生成查询添加

Left outer join tableX x ON x.id = main_table.x_id AND x.deleted_at IS NULL

即使 where 子句说 include x.deleted_at IS NULL OR x.deleted_at IS NOT NULL 因为它们在连接中被拒绝,所以它们不包括在内。

我正在使用 rails 4.2.3 和 paranoia 4.2.3 我尝试检查一些配置,过了一会儿发现当我使用 rails 4.2.1 时它工作正常。如果可能的话,我想保留 rails 4.2.3。

我应该怎么做才能解决我的问题?如果有人有想法,我准备尝试一下。 提前致谢。


我尝试添加 joins(:position)、joins(:member) 和两者,但结果是一样的

我试图改变

belongs_to :member, -> { with_deleted }

belongs_to :member, -> { with_deleted.unscope(where: :deleted_at) }
belongs_to :member, -> { unscope }

但它没有任何改变

在您的示例中,您正在使用 :includes 让 Rails 决定您是否要使用 :preload:eager_load:joins 并且因为您使用的是包含的 table 中的属性,所以它与 :joins 一起使用。 通过以上解释,我可以告诉您,这与 Rails https://github.com/rails/rails/issues/19226 中的一个当前未解决的错误有关,其中 "joined" table 忽略您试图覆盖 default_scope 的设置。我还没有发现任何解决方法