3

I have a project where I need to use internacionalization for table fields and to provide a jsonapi. To accomplish this I'm using the Globalize Gem and the JSON API Gem.

This is the code for a simple model called category:

class Category < ActiveRecord::Base
  translates :name
  validates :name, presence: true, uniqueness: { scope: :special_type }
end

class CategoryResource < JSONAPI::Resource
  attributes :name, :special_type
end

When making a simple request as /categories the response is fine:

{
  "data": [
    {
      "id": "1",
      "type": "categories",
      "links": {
        "self": "http://localhost:3000/v1/categories/1"
      },
      "attributes": {
        "name": "test",
        "special-type": "1"
      }
   },
  ...
  ]
}

But when trying to sort by name with `/categories?sort=name' I get an exception:

"PG::UndefinedColumn: ERROR: column categories.name does not exist\nLINE 1: SELECT \"categories\".* FROM \"categories\" ORDER BY \"categories...\n ^\n: SELECT \"categories\".* FROM \"categories\" ORDER BY \"categories\".\"name\" ASC"

as the column name is not in the table categories but on the table category_translations. What should I do to solve this?

Tiago
  • 673
  • 1
  • 8
  • 24
  • 1
    I'm not familiar with `JSON API Gem`, however, in order to sort the `categories` by name, you have to do: `Category.includes(:translations).order('category_translations.name')` – Roc Khalil Jul 05 '17 at 09:57
  • Thanks for your reply! Yes, I know that's how you do when you want to order something through globalize. But the JSONAPI-Resources gem seems to be ignoring this when getting a request with a sort option. – Tiago Jul 05 '17 at 11:13

0 Answers0