【问题标题】:Rails Include filtered model in Json objectRails 在 Json 对象中包含过滤后的模型
【发布时间】:2012-07-13 09:54:43
【问题描述】:

我有以下控制器代码

respond_with(@employees) do |format|
    format.json { render :json => @employees.to_json(:include => :shifts) }
end

如果我想过滤包含的班次,我该怎么办?例如按日期。 我必须能够在控制器中设置过滤器参数。

编辑: 我考虑过使用 :method 但它在 json 对象中创建了另一个变量。它必须被称为“转变”

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 json


    【解决方案1】:
    format.json { render :json => @employees.includes(:shifts).where("shifts.date > ?", your_date_here).to_json(:include => :shifts) }
    

    【讨论】:

    • 哇,非常感谢。但它也应该包括没有轮班的员工
    猜你喜欢
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 2019-11-25
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    相关资源
    最近更新 更多