Rails Globalize Gem + JSON Resources Gem

Viewed 248

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?

0 Answers
Related