Skip to content
  • Alex's avatar
    0654a614
    Ensure consistent sort order in `ActiveModel#serializable_hash` when using `only` option · 0654a614
    Alex authored
    
    
    This is a bit of a nitpick, but say you do this:
    
    ```ruby
    user.as_json(only: [:email, :name]).keys
    ```
    
    The result will depend on the order that the columns were added to your database (or in a test environment, the order of the columns in your `structure.sql`). ie. you might get `["email", "name"]` or `["name", "email"]`.
    
    In the specific case that you've provided `only` you are being explicit about which fields should be returned, so we can also be explicit about the order that they are returned in. This PR implements that. Now it will always return fields based on the order they're provided to the `only` argument, so in this case it would always be `["email", "name"]`.
    
    Co-authored-by: default avatarJohn Hawthorn <john@hawthorn.email>
    0654a614
    Ensure consistent sort order in `ActiveModel#serializable_hash` when using `only` option
    Alex authored
    
    
    This is a bit of a nitpick, but say you do this:
    
    ```ruby
    user.as_json(only: [:email, :name]).keys
    ```
    
    The result will depend on the order that the columns were added to your database (or in a test environment, the order of the columns in your `structure.sql`). ie. you might get `["email", "name"]` or `["name", "email"]`.
    
    In the specific case that you've provided `only` you are being explicit about which fields should be returned, so we can also be explicit about the order that they are returned in. This PR implements that. Now it will always return fields based on the order they're provided to the `only` argument, so in this case it would always be `["email", "name"]`.
    
    Co-authored-by: default avatarJohn Hawthorn <john@hawthorn.email>
Loading