我正在使用jqgrid_for_rails gem来设计数据库中的表。在我的表中,几个列数据来自虚拟属性。而我的jqgrid rails在loadonce => false(即,与单个时间不同,它使用分页加载数据)。
在我的搜索功能中,我必须提供searchField搜索内容,但它直接命中数据库列。所以我想将搜索功能应用到数组中。但不可能,因为默认情况下在ActiveRecord模型中搜索。所以我想将我的数组对象转换为数据库ActiveRecord Model。
怎么可能?
这是我的代码 - 帮助文件
def order_tracker_jqgrid
options = {:on_document_ready => true, :html_tags => false}
grid = [{
:url => '/order_tracker',
:datatype => 'json',
:loadonce => false,
:mtype => 'GET',
:height => 350,
:altRows => true,
:rownumbers => true,
:colNames => ['#' , 'Order #', 'Tracking #', 'Items', 'Delivery Date', 'Current Status', 'Pickup Location', 'Created By', 'Retailer'],
:colModel => [
{ :name => 'id', :index => 'id', :align => 'center', :search => false, :width => 100, :hidden => true},
{ :name => 'order_number', :index => 'order_number', :classes => 'order-number', :width => 100, :searchoptions => { :sopt => ['eq', 'cn']}},
{ :name => 'tracking_number', :index => 'tracking_number', :width => 110, :classes => 'tracking-number', :searchoptions => { :sopt => ['eq', 'cn']}},
{ :name => 'item_count', :index => 'item_count', :align => 'center' , :width => 80, :search => false},
{ :name => 'delivery_date_with_slot', :index => 'delivery_date_with_slot', :width => 200, :search => false},
{ :name => 'order_state', :index => 'order_state', :search => false},
{ :name => 'pickup_location_data', :index => 'pickup_location_data', :jsonmap => 'pickup_location_data', :search => false},
{ :name => 'creator_name', :index => 'creator_name', :search => false},
{ :name => 'company_name', :index => 'company_name', :search => false}
],
:pager => '#grid-pager',
:rowNum => 10,
:rowList => [10, 20, 30],
:sortname => 'id',
:sortorder => 'asc',
:viewrecords => true,
:autowidth => true,
:loadComplete => "function(){var table = this;setTimeout(function(){updatePagerIcons(table);}, 0);}".to_json_var,
:gridComplete => "function(){
var ids = jQuery('#grid-table').jqGrid('getRowData');
for(var i = 0; i < ids.length; i++ )
{
var id = ids[i].id;
var o_number = ids[i].order_number;
var t_number = ids[i].tracking_number;
var order_link = '<a href=\"/orders/'+id+'/show_incomming_order\" class=\"details fancybox fancybox.ajax\">'+o_number+'</a>';
var tracker_link = '<a href=\"/order/'+t_number+'\" class=\"fancybox fancybox.ajax\">'+t_number+'</a>';
$('#grid-table tr#'+id).find('.order-number').html(order_link);
$('#grid-table tr#'+id).find('.tracking-number').html(tracker_link);
}
}".to_json_var
}]
pager = [:navGrid, "#grid-pager", {:del => false, :add => false, :edit => false, :searchicon => 'icon-search orange', :refreshicon => 'icon-refresh blue'}, {:closeAfterEdit => true, :closeOnEscape => true}, {}, {}, {}, {}]
jqgrid_api 'grid-table', grid, pager, options
结束
在我的控制器上
#If current user is fulflmnt admin
@orders = OrderBucket.orders_from_cate2_3.where(search_params).order("created_at desc") #("created_at >= ? AND created_at <= ?", Time.zone.now.beginning_of_month, Time.zone.now.end_of_month).order("created_at desc")
@columns = ['id' , 'order_number' , 'tracking_number', 'item_count', 'delivery_date_with_slot', 'order_state', 'pickup_location_data', 'creator_name', 'company_name']
if params[:_search] == "true"
@orders = @orders.search(params[:_search],params[:searchField],params[:searchString], params[:page],params[:rows],params[:searchOper],params[:sord])
else
sort_params = params[:sidx]
if sort_params == "order_number"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.order_number}.flatten : @orders.sort_by{|p| p.order_number}.reverse.flatten
elsif sort_params == "tracking_number"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.tracking_number}.flatten : @orders.sort_by{|p| p.tracking_number}.reverse.flatten
elsif sort_params == "pickup_location_data"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.pickup_location_data}.flatten : @orders.sort_by{|p| p.pickup_location_data}.reverse.flatten
elsif sort_params == "order_state"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.order_state}.flatten : @orders.sort_by{|p| p.order_state}.reverse.flatten
elsif sort_params == "creator_name"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.creator_name}.flatten : @orders.sort_by{|p| p.creator_name}.reverse.flatten
elsif sort_params == "company_name"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.company_name}.flatten : @orders.sort_by{|p| p.company_name}.reverse.flatten
elsif sort_params == "delivery_date_with_slot"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.delivery_date_with_slot}.flatten : @orders.sort_by{|p| p.delivery_date_with_slot}.reverse.flatten
elsif sort_params == "item_count"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.item_count}.flatten : @orders.sort_by{|p| p.item_count}.reverse.flatten
end
@orders = @orders.paginate(
:page => params[:page],
:per_page => params[:rows],
:order => order_by_from_params(params))
end
if request.xhr?
render :json => json_for_jqgrid(@orders, @columns)
end
和我的模特
def self.search(_search,searchField,searchString,page,rows,searchOper,sord)
if searchOper == 'cn' && _search # contains
wildcard_search = "%#{searchString}%"
where("#{searchField} LIKE :search", search: wildcard_search).page(page).per_page(rows)
elsif searchOper == 'nc' && _search # does not contain
wildcard_search = "%#{searchString}%"
where.not("#{searchField} LIKE :search", search: wildcard_search).page(page).per_page(rows)
elsif searchOper == 'bw' && _search # begin with
wildcard_search = "#{searchString}%"
where("#{searchField} LIKE :search", search: wildcard_search).page(page).per_page(rows)
elsif searchOper == 'bn' && _search # does not begin with
wildcard_search = "#{searchString}%"
where.not("#{searchField} LIKE :search", search: wildcard_search).page(page).per_page(rows)
elsif searchOper == 'en' && _search # does not end with
wildcard_search = "%#{searchString}"
where.not("#{searchField} LIKE :search", search: wildcard_search).page(page).per_page(rows)
elsif searchOper == 'ew' && _search # ends with
wildcard_search = "%#{searchString}"
where("#{searchField} LIKE :search", search: wildcard_search).page(page).per_page(rows)
elsif searchOper == 'eq' && _search # equal
where("#{searchField} = :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'ne' && _search # not equal
where.not("#{searchField} = :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'in' && _search # is in
# where("#{searchField} = :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'ni' && _search # is in
# where("#{searchField} = :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'lt' && _search # less
where("#{searchField} < :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'le' && _search # less or equal
where("#{searchField} <= :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'gt' && _search # greater
where("#{searchField} > :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'ge' && _search # greater or equal
where("#{searchField} >= :search", search: searchString).page(page).per_page(rows)
else
wildcard_search = "%#{searchString}%" # contains
where("name LIKE :search", search: wildcard_search).page(page).per_page(rows)
end
端
答案 0 :(得分:0)
我想我得到的东西可行。我从来没有使用它,所以我不能保证它的稳定性(如果你使用它我想要你的反馈)
因此,让我们使用User
模型运行此示例。你有一个只有user
个记录的数组
array = [User.find(5), User.find(50), User.find(500)]
您可以创建一个空的关系对象
# Rails 4: `none` is going to be the easiest and most efficient
relation = User.none
# Rails 3: we have to cheat
relation = User.where('1 = 0')
逐个添加
relation << array[0]
relation << array[2]
或者一下子
relation += array
答案 1 :(得分:0)
要将数组对象转换为ActiveRecord关系对象,只需执行以下操作:
MyModel.where(id: my_arr.map(&:id))
MyModel
是数组中对象arr
是数组对象