order_values API visibility and ActiveRecord order? (ordered?) proposal

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

order_values API visibility and ActiveRecord order? (ordered?) proposal

Josef Šimánek-2
Hello!

I'm facing problem where I need to check if active record relation is already ordered and if not I would like to order by some default column (id in my case).

Right now I'm using "scope.order_values.any?" to find out current ordering state and decide if we need to specify ordering or not.

Anyway I'm not sure how to find out if order_values is actual public API. It is generated method (source - https://github.com/rails/rails/blob/cb6db15d221d261bf5060c8752ba3b8b28e3b651/activerecord/lib/active_record/relation/query_methods.rb#L77) and it is not visible at https://api.rubyonrails.org/. But it doesn't seems for me since it also returns array of arel nodes.

I had an idea to contribute "order?" or "ordered?" to public API to be able to find out if relation is already ordered.

I have two questions:

1. Is order_values public API and safe to use in our Rails application? If so, is there any way how to make it documented?
2. If not, would be order? or ordered? method welcomed as new query method? Implementation is really simple.

Josef

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/e73b5784-2015-4fa4-9612-30ceeb34fa46%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: order_values API visibility and ActiveRecord order? (ordered?) proposal

"Martin J. Dürst"
Hello Josef,

On 2019/09/26 09:16, Josef Šimánek wrote:
> Hello!
>
> I'm facing problem where I need to check if active record relation is
> already ordered and if not I would like to order by some default column (id
> in my case).

Why do you need to check? Just say what order you need, and the database
will figure out whether it needs to do actual work or can skip ordering.

Regards,   Martin.

> Right now I'm using "scope.order_values.any?" to find out current ordering
> state and decide if we need to specify ordering or not.
>
> Anyway I'm not sure how to find out if order_values is actual public API.
> It is generated method (source -
> https://github.com/rails/rails/blob/cb6db15d221d261bf5060c8752ba3b8b28e3b651/activerecord/lib/active_record/relation/query_methods.rb#L77)
> and it is not visible at https://api.rubyonrails.org/. But it doesn't seems
> for me since it also returns array of arel nodes.
>
> I had an idea to contribute "order?" or "ordered?" to public API to be able
> to find out if relation is already ordered.
>
> I have two questions:
>
> 1. Is order_values public API and safe to use in our Rails application? If
> so, is there any way how to make it documented?
> 2. If not, would be order? or ordered? method welcomed as new query method?
> Implementation is really simple.
>
> Josef
>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/31e0a380-df49-085f-03f6-2aeda6b17839%40it.aoyama.ac.jp.
Reply | Threaded
Open this post in threaded view
|

Re: order_values API visibility and ActiveRecord order? (ordered?) proposal

Joshua Flanagan
I do not think this is a good idea to add to the framework. I can imagine “ordered” as a scope on models in various applications.
You can add it your ApplicationRecord.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/d8a4f0a1-d2ca-4b75-9024-05d0bd6b2336%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

order_values API visibility and ActiveRecord order? (ordered?) proposal

Yves-Eric Martin-4
In reply to this post by Josef Šimánek-2
Hi Josef,

There are good reasons for keeping APIs footprint small and not adding unnecessary public methods, even if they may be easy to implement. So before talking implementation, maybe making a strong case for why this `ordered?` would be a good addition to the Rails API would help your idea get traction.

So like others I must ask: why do you need this? I tried to think of some use cases, but all of them could be achieved by, instead of trying to add a default order after your query is mostly built, by setting the default order first, then reordering later when the code calls for it. This way, if your conditional logic did not reorder, the default order makes it through.

Also, I am not convinced the implementation, or even the definition of this `ordered?` method is that easy. It may be for the simple single model query case. But now throw in some relations, joins and includes, some of these ordered, some others not. What should `ordered?` return?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/e8e90ad4-ae77-4e23-85dd-ddcf0ed39239%40googlegroups.com.