所以我想动态地将过滤器参数传递给我的 where 方法所以基本上我有这个
@colleges = College.where(@filter).order(@sort_by).paginate(:page => params[:page], :per_page => 20)
而@where 只是用这个方法构建的一个字符串
def get_filter_parameters
if params[:action] == 'index'
table = 'colleges'
columns = College.column_names
else
table = 'housings'
columns = Housing.column_names
end
filters = params.except(:controller, :action, :id, :sort_by, :order, :page, :college_id)
filter_keys = columns & filters.keys
@filter = ""
first = true
if filter_keys
filter_keys.each do |f|
if first
@filter << "#{table}.#{f} = '#{filters[f]}'"
first = false
else
@filter << " AND #{table}.#{f} = '#{filters[f]}'"
end
end
else
@filter = "1=1"
end
问题是我不知道将原始 SQL 放到这样的 where 方法中有多好。我知道通常你可以做 :state => 'PA' 之类的事情,但我该如何动态地做到这一点?
更新
好的,所以我现在传递一个哈希并拥有这个:
if params[:action] == 'index'
columns = College.column_names
else
columns = Housing.column_names
end
filters = params.except(:controller, :action, :id, :sort_by, :order, :page, :college_id)
filter_keys = columns & filters.keys
@filter = {}
if filter_keys
filter_keys.each do |f|
@filter[f] = filters[f]
end
end
这足以防止注射吗?