-
Notifications
You must be signed in to change notification settings - Fork 3
How to sort when default scope is ordered #83
Copy link
Copy link
Open
Description
When you use standardapi to query records whose model has a default scope, there is a potential that the default scope interferes with the expected result. Oftentimes this can be solved just by tweaking the default_scope. But is there a potential fix for this that allows you to maybe unscope?
It's possible that this isn't a standardapi fix. But wanted to pose the question regardless.
Here are the details
When you add a default scope to a model that uses the order method...
class Order < ApplicationRecord
default_scope { order(created_at: :desc) }
end...it does the following:
irb(main):001:0> Order.all.order(updated_at: :desc).limit(1)
Order Load (8.7ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."created_at" DESC, "orders"."updated_at" DESC LIMIT $1 [["LIMIT", 1]]This can be solved by using unscoped:
irb(main):002:0> Order.all.unscoped.order(updated_at: :desc).limit(1)
Order Load (4.3ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."updated_at" DESC LIMIT $1 [["LIMIT", 1]]This can either be solved by removing the default scope or somehow telling tthe activerecord-filter to unscope the model before applying the filter parameters.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels