How to return matched results in ActiveRecord and then remainder?

Rumel

Say we have data like

< id: 1, name: "Bill", type: "Lemur" >
< id: 2, name: "Bob", type: "Cow" >
< id: 3, name: "Nancy", type: "Lemur" >
< id: 4, name: "Jack", type: "Seagull" >
< id: 5, name: "Jill", type: "Seagull" >
< id: 6, name: "Jake", type: "Cow" >
< id: 7, name: "Jess", type: "Lemur" >
< id: 8, name: "Nick", type: "Lemur" >
< id: 9, name: "Jacky", type: "Cow" >
< id: 10, name: "Jerry", type: "Cow" >
< id: 11, name: "Samuel", type: "Seagull" >
< id: 12, name: "Tessa", type: "Lemur" >

I want to return all results where type is Lemur first and then return the rest of the results in any order. I thought I could use the group method of ActiveRecord but I don't think I understood that correctly and it's doing something else. I've tried doing Animal.order('Field(type, "Lemur")') after seeing another Stack Overflow answer but that didn't work either. I need a relation returned as I'm going to pass this on to Kaminari to paginate. Anyone know how to do this?

To make things more complicated we actually want to do this on an hstore column but I wanted to figure out the general answer first.

This is on Rails 3.2 with Postgres 9.3.

SomeDudeSomewhere

Since Postgres doesn't have an "ORDER BY FIELD(some_column, 'Zebra', 'Hipo')" like MySQL does - you could try to use "Order by bang!", like such:

Animal.order("type!='Lemur', type!='Cow', type!='Seagull'")

As suggested on this post:

Simulating MySQL's ORDER BY FIELD() in Postgresql

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select2 4.0.1: How to return only first character matched results?

From Dev

Return a query with nested results with Rails ActiveRecord

From Dev

ActiveRecord .last and ActiveRecord .last(1) return different results

From Dev

How is it possible that SQL query and ActiveRecord.find_by_sql return different results?

From Dev

How to get "remainder" of query (all non-matched pairs) in LINQ and collect it in a list

From Dev

How to return matched regex in R gsub

From Dev

How to return pattern-matched element?

From Dev

How to return matched value with .NET regex?

From Dev

How to find by array and return nil if not matched?

From Dev

How to return 0 if a pattern is matched from a file?

From Dev

How to return pattern-matched element?

From Dev

How to return 0 if a pattern is matched from a file?

From Dev

How to return the column head if a cell is matched?

From Dev

How to return matched regex in R gsub

From Dev

How to delete/destroy an activerecord results set?

From Dev

How do I explore the results of an ActiveRecord relation?

From Dev

How to sort ActiveRecord results by associated model count?

From Dev

How do I exclude search results in ActiveRecord?

From Dev

How do I explore the results of an ActiveRecord relation?

From Dev

No results matched the query

From Dev

Select with IN, filtering the matched results

From Dev

How to return ActiveRecord Association Proxy on ERB

From Dev

Excel: Find results in column, and then search all matched rows for another value and return sum

From Dev

How to return just the matched elements from a mongoDB array

From Dev

grep - how to return only matched part of string on a column

From Dev

Compare two arrays and return their remainder

From Dev

Division remainder return 2 zero

From Dev

How to return results from a JobService?

From Dev

How to return results from a JobService?

Related Related

  1. 1

    Select2 4.0.1: How to return only first character matched results?

  2. 2

    Return a query with nested results with Rails ActiveRecord

  3. 3

    ActiveRecord .last and ActiveRecord .last(1) return different results

  4. 4

    How is it possible that SQL query and ActiveRecord.find_by_sql return different results?

  5. 5

    How to get "remainder" of query (all non-matched pairs) in LINQ and collect it in a list

  6. 6

    How to return matched regex in R gsub

  7. 7

    How to return pattern-matched element?

  8. 8

    How to return matched value with .NET regex?

  9. 9

    How to find by array and return nil if not matched?

  10. 10

    How to return 0 if a pattern is matched from a file?

  11. 11

    How to return pattern-matched element?

  12. 12

    How to return 0 if a pattern is matched from a file?

  13. 13

    How to return the column head if a cell is matched?

  14. 14

    How to return matched regex in R gsub

  15. 15

    How to delete/destroy an activerecord results set?

  16. 16

    How do I explore the results of an ActiveRecord relation?

  17. 17

    How to sort ActiveRecord results by associated model count?

  18. 18

    How do I exclude search results in ActiveRecord?

  19. 19

    How do I explore the results of an ActiveRecord relation?

  20. 20

    No results matched the query

  21. 21

    Select with IN, filtering the matched results

  22. 22

    How to return ActiveRecord Association Proxy on ERB

  23. 23

    Excel: Find results in column, and then search all matched rows for another value and return sum

  24. 24

    How to return just the matched elements from a mongoDB array

  25. 25

    grep - how to return only matched part of string on a column

  26. 26

    Compare two arrays and return their remainder

  27. 27

    Division remainder return 2 zero

  28. 28

    How to return results from a JobService?

  29. 29

    How to return results from a JobService?

HotTag

Archive