Rails 控制器索引排除记录



我在工作订单控制器中有以下索引:

def index
   @workorders = Workorder.scoped
   @workorders = Workorder.where("created_at > #{params[:after]}") if params[:after].present?
end

我将如何添加以下内容以使其成为 AND。 我希望它只在最大同步<>"E"时才包含工作订单。 即使参数 :after 是否存在。

我试过这个:

@workorders = Workorder.scoped.where("maxsynch != 'E' ")

感谢您的帮助!

您可以像这样链接作用域:

def index
   @workorders = Workorder.scoped.where("maxsynch != 'E' ")
   @workorders = @workorders.where("created_at > ?", params[:after]) if params[:after].present?
   #examples:
   @workorders = @workorders.where(parent_id: params[:parent_id]) if params[:parent_id].present?
   @workorders = @workorders.active if params[:filter_active].present?
end

最新更新