【问题标题】:Apply multiple filters JSON API Resources应用多个过滤器 JSON API 资源
【发布时间】:2017-04-08 12:28:31
【问题描述】:

只是一个我无法弄清楚的简单问题。我想在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 并将上述代码和网址中的filters 更改为filter,则它将正常工作。

【问题讨论】:

    标签: ruby-on-rails jsonapi-resources


    【解决方案1】:

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

    1. 网址应该有过滤器关键字而不是过滤器。即。

        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&amp;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-03-05
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 2017-02-19
      • 2017-03-30
      相关资源
      最近更新 更多