1

只是一个我无法弄清楚的简单问题。我想在rails JsonApiResources. 这是我的资源代码。

      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)
      }

这是我打电话的网址。

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

但它不调用此代码,而是self.records调用。但是,如果我将代码更改为仅支持一个参数,假设:start_date并更改filtersfilter上面的代码和 url,那么它将正常工作。

4

1 回答 1

0

我对过滤器也有一些问题。所以,我使用了两个单独的过滤器来实现它。

  1. url 应该有过滤器关键字而不是过滤器。IE。

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

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

  2. 在资源文件中:

    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])
    }
    
于 2017-10-10T09:50:24.490 回答