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

时间:2015-08-17 18:11:00

标签: ruby-on-rails model ruby-paranoia

这是我到目前为止所得到的:

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

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

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

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

即使使用where子句,其中包含x.deleted_at IS NULL或x.deleted_at IS NOT NULL,因为它们在连接中被拒绝,因此不包括它们。

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

我该怎么做才能解决我的问题?如果有人有想法我就准备尝试了。 提前谢谢。

我尝试添加连接(:position),连接(:member)和两者但结果是相同的

我试图改变

belongs_to :member, -> { with_deleted }

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

但它什么都没改变

1 个答案:

答案 0 :(得分:0)

在您的示例中,您正在使用:includes,让Rails决定您是否要使用:preload:eager_load:joins,因为您是使用包含的表中的属性,然后它与:joins一起使用。 通过上面的解释,我可以告诉你,这与Rails https://github.com/rails/rails/issues/19226中当前的开放错误有关,其中“加入”表忽略了你试图覆盖{{1}的设置}。我还没有解决方法