【发布时间】:2015-04-03 13:10:45
【问题描述】:
我构建了一个 Rails 应用程序作为移动应用程序的 API。 我想根据 GET 参数获取记录的子集。
我的模型是这样的:
# == Schema Information
#
# Table name: inspections
#
# id :integer not null, primary key
# date :date
# station_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Inspection < ActiveRecord::Base
include Filterable
belongs_to :station
has_many :state_checks
scope :station_id, -> (station_id) { where station_id: station_id }
scope :date, -> (date) { where date: DateTime.parse(date) }
end
然后我写了一个关注:
module Filterable
extend ActiveSupport::Concern
module ClassMethods
def filter(filtering_params)
results = self.where(nil)
filtering_params.each do |key, value|
results = results.public_send(key, value) if value.present?
end
results
end
end
end
现在,当我执行 GET '/inspections.json?station_id=1&date=2015-04-03T10:44:00.000Z' 范围 :station_id 有效,但 :date 范围无效。 有什么线索吗?
谢谢
【问题讨论】:
-
嘿,你的表格上有什么?
标签: ruby-on-rails datetime get params