2014-07-20 118 views
0

我正在使用jqgrid_for_rails gem從數據庫設計表。在我的表格中,很少有列數據來自虛擬屬性。和我的jqgrid軌道loadonce => false(即它不同的單次加載數據與分頁)。如何將數組對象轉換爲ActiveRecord關係對象

在我的搜索功能中,我必須提供searchField搜索的內容,但它直接敲擊數據庫列。所以我想將搜索功能應用到數組中。但不可能,因爲它默認在ActiveRecord模型中搜索。所以我想將我的數組對象轉換爲數據庫ActiveRecord模型。

這怎麼可能?

這裏是我的代碼 - 輔助文件

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 

,並在我的模型

搜索方法找到jqGrid的順序表數據

高清self.search(_search,searchField,搜索字符串,頁,行,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

顯示你現有的一些代碼w我會幫忙的! – emaillenin

+0

嘿。我更新了代碼。請立即檢查 – Shubhankar

回答

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 
添加逐一

relation << array[0] 
relation << array[2] 

或者全部

+0

嘿。我用我的代碼更新了它。你現在可能會更清楚一些想法。我已經試過你的代碼。但在最後一步「關係+ =數組」它會自動轉換成數組對象 – Shubhankar

+0

所以你的意思是'(關係+ =數組)「.class.name'輸出'數組'?奇怪的。但是對於我嘗試過的小程序來說,它在Rails 4中按預期工作。升級項目的任何更改都會發生? (在此之前您需要完整的測試覆蓋率) – Benj

相關問題