Allow to use rails style on queries with specific order

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

Allow to use rails style on queries with specific order

Pablo Margreff
I have a situation which I need use a specific order for queries which the attributes are an enum, something like this: 
.order("x.status = 5, x.status = 1, x.status = 6 ..."), but it isn't the best way for readability, and I haven't found anything about how to do it on rails way.

Is a big deal create this functionality on rails? Something like:
.order(status: [:removed, :new, :active, :corrupted ...])

If yes, it's something trivial, if a haven't contributed with rails, can I help to build that?




--
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 post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Allow to use rails style on queries with specific order

Kevin Deisz
Are you using MySQL? It has a field function that solves this 


You could probably do the same thing with substring index with other DBMSs.

On Thu, Oct 19, 2017 at 1:30 PM Pablo Margreff <[hidden email]> wrote:
I have a situation which I need use a specific order for queries which the attributes are an enum, something like this: 
.order("x.status = 5, x.status = 1, x.status = 6 ..."), but it isn't the best way for readability, and I haven't found anything about how to do it on rails way.

Is a big deal create this functionality on rails? Something like:
.order(status: [:removed, :new, :active, :corrupted ...])

If yes, it's something trivial, if a haven't contributed with rails, can I help to build that?




--
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 post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
--
Kevin D. Deisz
Localytics Software Engineer

--
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 post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Allow to use rails style on queries with specific order

Martin Hong
In reply to this post by Pablo Margreff
I just meet the same problem like you

ORDER_PRIORITIES = [:removed, :new, :active, :corrupted]
def self.priorities_based_order
  conditions
= ORDER_PRIORITIES.each_with_index.map { |value, index| "WHEN #{value} THEN #{index} ELSE 999" }.join("\n") # I want other statuses to be sorted lastly, you can also use FIELD function in MySQL
  order_by
("CASE `#{table_name}`.`status` #{conditions} END")
end

# usage:
user
.orders.priorities_based_order
The above codes not run yet but I think it must work.


在 2017年10月20日星期五 UTC+8上午1:30:35,Pablo Margreff写道:
I have a situation which I need use a specific order for queries which the attributes are an enum, something like this: 
.order("x.status = 5, x.status = 1, x.status = 6 ..."), but it isn't the best way for readability, and I haven't found anything about how to do it on rails way.

Is a big deal create this functionality on rails? Something like:
.order(status: [:removed, :new, :active, :corrupted ...])

If yes, it's something trivial, if a haven't contributed with rails, can I help to build that?




--
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 post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Allow to use rails style on queries with specific order

Pablo Margreff
In reply to this post by Kevin Deisz
Nop, I'm using postgres, but I already solved the problem, my point here is implement something on the rails way ...

On Thursday, October 19, 2017 at 3:32:58 PM UTC-2, Kevin Deisz wrote:
Are you using MySQL? It has a field function that solves this 

<a href="https://www.w3schools.com/sql/func_mysql_field.asp" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.w3schools.com%2Fsql%2Ffunc_mysql_field.asp\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEgnb2uI1xdt3qmjTc7jPXSjGmyLg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.w3schools.com%2Fsql%2Ffunc_mysql_field.asp\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEgnb2uI1xdt3qmjTc7jPXSjGmyLg&#39;;return true;">https://www.w3schools.com/sql/func_mysql_field.asp

You could probably do the same thing with substring index with other DBMSs.

On Thu, Oct 19, 2017 at 1:30 PM Pablo Margreff <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="HGD7WO8IBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">pmar...@...> wrote:
I have a situation which I need use a specific order for queries which the attributes are an enum, something like this: 
.order("x.status = 5, x.status = 1, x.status = 6 ..."), but it isn't the best way for readability, and I haven't found anything about how to do it on rails way.

Is a big deal create this functionality on rails? Something like:
.order(status: [:removed, :new, :active, :corrupted ...])

If yes, it's something trivial, if a haven't contributed with rails, can I help to build that?




--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="HGD7WO8IBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-co...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="HGD7WO8IBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;">https://groups.google.com/group/rubyonrails-core.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.
--
Kevin D. Deisz
Localytics Software Engineer

--
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 post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Allow to use rails style on queries with specific order

Pablo Margreff
In reply to this post by Martin Hong
Hey, thanks for the answer. My final solution looks something like that, but How I said to Kevin, my point here is implement it on rails to allow use it on rails style.

The problem is use something like :removed, :new, etc isn't allowed because rails always expect ASC or DESC for param for order, do u get my point? I haven't any exp with rails core, but is that something that should be allowed if I open a PR?  and that's something for a first-time traveler implements? 

On Friday, October 20, 2017 at 2:47:41 AM UTC-2, Martin Hong wrote:
I just meet the same problem like you

ORDER_PRIORITIES = [:removed, :new, :active, :corrupted]
def self.priorities_based_order
  conditions
= ORDER_PRIORITIES.each_with_index.map { |value, index| "WHEN #{value} THEN #{index} ELSE 999" }.join("\n") # I want other statuses to be sorted lastly, you can also use FIELD function in MySQL
  order_by
("CASE `#{table_name}`.`status` #{conditions} END")
end

# usage:
user
.orders.priorities_based_order
The above codes not run yet but I think it must work.


在 2017年10月20日星期五 UTC+8上午1:30:35,Pablo Margreff写道:
I have a situation which I need use a specific order for queries which the attributes are an enum, something like this: 
.order("x.status = 5, x.status = 1, x.status = 6 ..."), but it isn't the best way for readability, and I haven't found anything about how to do it on rails way.

Is a big deal create this functionality on rails? Something like:
.order(status: [:removed, :new, :active, :corrupted ...])

If yes, it's something trivial, if a haven't contributed with rails, can I help to build that?




--
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 post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.