我需要在rails中生成一个收据列表,需要按商品的订单关系字段(payment_method_meta_type.name
)排序。
模特:
Receipt
Deposit
PaymentMethodMetaType
存款模式:
class Deposit < ActiveRecord::Base
belongs_to :payment_method_meta_type
has_many :receipts, :class_name=>"Receipt", :foreign_key=>"deposit_id",
:dependent => :destroy
end
我已经在控制器中收到了一个收据数组:
@receipts = Receipt.find(:all, :conditions => ["date BETWEEN ? AND ?",
@start_date, @end_date], :order => "date DESC, id DESC",
:limit => limit, :offset => offset)
在视图中,我也可以显示payment_method_meta_type.name
- @receipts.each do |o|
%tr.
.....
%td #{o.receipt_number}
%td #{o.deposit.payment_method_meta_type.name}
.....
但是,当我创建收据数组的集合时,如何在控制器中按receipts.deposit.payment_method_meta_type.name
的顺序显示列表?
答案 0 :(得分:0)
试试这个:
@receipts = Receipt.all(:joins => {:deposit => :payment_method_meta_type},
:conditions => {:date => @start_date..@end_date},
:order => "payment_method_meta_types.name ASC",
:limit => limit, :offset => offset)
答案 1 :(得分:0)
thx,我终于解决了,在查询字符串中使用'include'
@receipts = Receipt.find(:all, :include => {:deposit => [:payment_method_meta_type] } ,:conditions => ["Receipts.business_date BETWEEN ? AND ?", @current_month_start_date, @current_month_end_date],:order => "tag_types.name , Receipts.business_date DESC",:limit => limit)