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.