Skip to content
  • Kevin Newton's avatar
    459ca225
    Add `ActiveRecord::QueryMethods#in_order_of`. · 459ca225
    Kevin Newton authored
    This allows you to specify an explicit order that you'd like records
    returned in based on a SQL expression. By default, this will be accomplished
    using a case statement, as in:
    
    ```ruby
    Post.in_order_of(:id, [3, 5, 1])
    ```
    
    will generate the SQL:
    
    ```sql
    SELECT "posts".* FROM "posts" ORDER BY CASE "posts"."id" WHEN 3 THEN 1 WHEN 5 THEN 2 WHEN 1 THEN 3 ELSE 4 END ASC
    ```
    
    However, because this functionality is built into MySQL in the form of the
    `FIELD` function, that connection adapter will generate the following SQL
    instead:
    
    ```sql
    SELECT "posts".* FROM "posts" ORDER BY FIELD("posts"."id", 1, 5, 3) DESC
    ```
    
    *Kevin Newton*
    459ca225
    Add `ActiveRecord::QueryMethods#in_order_of`.
    Kevin Newton authored
    This allows you to specify an explicit order that you'd like records
    returned in based on a SQL expression. By default, this will be accomplished
    using a case statement, as in:
    
    ```ruby
    Post.in_order_of(:id, [3, 5, 1])
    ```
    
    will generate the SQL:
    
    ```sql
    SELECT "posts".* FROM "posts" ORDER BY CASE "posts"."id" WHEN 3 THEN 1 WHEN 5 THEN 2 WHEN 1 THEN 3 ELSE 4 END ASC
    ```
    
    However, because this functionality is built into MySQL in the form of the
    `FIELD` function, that connection adapter will generate the following SQL
    instead:
    
    ```sql
    SELECT "posts".* FROM "posts" ORDER BY FIELD("posts"."id", 1, 5, 3) DESC
    ```
    
    *Kevin Newton*
Loading