发布于 2015-10-30 18:05:21
卢帕的博客 (@edelpero)把我推到了正确的方向,但我觉得要么我误解了什么,要么文档可能会改进(可能是前者)。也许下面是一些类型的-o,因为我正在从一个应用程序中简化,但是这里有一个解决方案:
表格
= form_tag pups_path, method: :get do
= check_box_tag 'male', 'm'
= check_box_tag 'female', 'f'
# add a hidden field so that the gender method in the scope class is called
= hidden_field_tag 'gender', 'anything' # tag must have a value
= submit_tag :search控制器
class PupsController < ApplicationController
def index
@pups = PupSearch.new(Pup.all).search(search_params)
end
protected
def search_params
params.permit(:male, :female, :gender) # permit all three
end
end作用域类
class WalkSearch < Lupa::Search
class Scope
def male
# leave blank to avoid chaining; handled in gender below
end
def female
# leave blank to avoid chaining; handled in gender below
end
def gender
# standard procedure for OR logic
scope.joins(:pup).where('pups.male_female' => [
search_attributes[:male],
search_attributes[:female]
])
end
end
endhttps://stackoverflow.com/questions/33427644
复制相似问题