1

just a simple question which I am unable to figure out. I want to apply multiple filters in rails JsonApiResources. Here's the resources code I've.

      filters :start_date, :end_date, apply: -> (records, value, _options){
        byebug
        time = ' 00:00:00'
        _options[:context][:current_company].documents.where('updated_at > ? AND updated_at < ?',value[0]+time,value[1]+time)
      }

This is the url I am calling.

http://localhost:3000/api/v1/dashboard?utf8=%E2%9C%93&filters%5Bstart_date%5D=2017-01-01&filters%5Bend_date%5D=2017-01-01&%5Bwith_tax%5D=0&commit=Search

But it doesn't call this code, instead self.records is called. But If I change the code to support only one parameter, let's say :start_date and change filters to filter in the above code and the url, than it will work fine.

Mj1992
  • 3,404
  • 13
  • 63
  • 102

1 Answers1

0

I had some issue with filters as well. So, I used two separate filter to achieve it instead.

  1. The url should have filter keyword instead of filters. ie.

      json_api :get, api_tags_path, filter: {
        'start_date': 'start-date' ,
        'end_date': 'end-date',
      }
    

    Example: http://www.example.com/api?filter[start_date]=start-date&filter[end_dat]=end-date

  2. And in the resource file:

    filter "start_date", apply: -> (records, value, _options){
         records.where('updated_at > ?', value[0])
    }
    
    filter "end_date", apply: -> (records, value, _options){
          records.where('updated_at < ?', value[0])
    }
    
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
ranendra
  • 2,492
  • 2
  • 19
  • 29