문제

I'm using the paranoia gem to "soft-delete" records. Now I need to eager load these records, some of which might have been deleted, for an associated model. Paranoia adds this default_scope to the "paranoid" model:

default_scope :conditions => { :deleted_at => nil }

So in effect, I have these (simplified) models:

class Product
  has_many :orders
  default_scope :conditions => { :deleted_at => nil }
end

class Order
  belongs_to :product
end

What I'm trying to achieve is to eager-load the products when accessing orders:

Order.includes(:product)

This (from How to use unscoped on associated relations in Rails3?) does not work here:

Product.unscoped { Order.includes(:product) }

I know I could create a custom belongs_to relationship to add conditions (as in Eager loading nested association and scope), but I can't find a way to remove existing ones, if that's even possible.

Question: How do I prevent the default scope from being applied to the eager loading query?

도움이 되었습니까?

해결책

Well, it turns out the workaround is to force a join on the "paranoid" model, which circumvents the default_scope:

Order.joins(:product).includes(:product)

Not pretty, but it works. Would like a better answer if possible.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top