2016-04-04 20 views
0

modelindeki bir tanım yöntemine dayanarak, uygulamam için ransack kullanıyorum ve sort_link ile bir tablo başlığı sortable oluşturmaya çalışıyorum, ancak sort_link'i tabloyu içeren tablo başlığına uyguladığımda sıkışıp kaldım. değil veritabanının tablosundan ancak modelde yönteminden değer ...ransack sort_link,

bu benim kod

modelim

def final_price_with_items 
    self.item_cost + self.final_price 
end 

ve ben bir ilan etmek benim modelinde beyaz listeye eklenmiş oldu tablodaki değeri içeren diğer tablo kafası veri tabanı.

self.whitelisted_ransackable_attributes = ['number','state', 'cost', 'created_at'] 

benim html

<table class="index" id="listing_shipments" data-hook> 
    <thead> 
    <tr data-hook="admin_shipments_index_headers"> 
     <th><%= sort_link @search, :created_at, Spree::Shipment.human_attribute_name(:created_at) %></th> 
     <th><%= sort_link @search, :number, Spree::Shipment.human_attribute_name(:number) %></th> 
     <th><%= sort_link @search, :state, Spree::Shipment.human_attribute_name(:state) %></th> 
     <th><%= sort_link @search, :cost, Spree::Shipment.human_attribute_name(:cost) %></th> 
     <th><%= sort_link @search, :final_price_with_items, Spree::Shipment.human_attribute_name(:final_price) %></th> 
     <th data-hook="admin_shipments_index_header_actions" class="actions"></th> 
    </tr> 
    </thead> 
    <tbody> 
    <% @shipments.each do |shipment|%> 
     <tr id="<%= spree_dom_id shipment %>" data-hook="admin_shipments_index_rows" class="<%= cycle('odd', 'even')%>"> 
     <td><%= l shipment.created_at.to_date %></td> 
     <td><%= link_to shipment.number, edit_admin_order_path(shipment.order) %></td> 
     <td><span class="state <%= shipment.state.downcase %>"><%= Spree.t("shipment_state.#{shipment.state.downcase}") %></span></td> 
     <td><%= shipment.display_cost.to_html %></td> 
     <td><%= Spree::Money.new(shipment.final_price_with_items, currency: shipment.currency).to_html %></td> 
     <td data-hook="admin_shipments_index_row_actions" class="actions align-center"> 
      <%= link_to_edit_url edit_admin_order_path(shipment.order), :title => "admin_edit_#{dom_id(shipment)}", :no_text => true %> 
     </td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

veritabanında sevkiyat tabloda, bu sütun vardı, ama final_price_with_items kimse bana yardımcı olabilir .. sıralama için çalışmaz, çünkü sıralama aklisteli mümkün beyan masa başı bu problemi çözmek için ?

cevap

0

Bir ransacker ile yapabilmelidir.

ransacker :final_price_with_items_sort do Arel.sql('item_cost + final_price') end

: Belki Sonra modelinde bu eklemek final_price_with_items_sort

için sıralama bağlantının adını değiştirmek istiyorum