我有一個小問題,我的where子句。Rails 3使用總和()在哪裏條款
PurchasePosition.where(:purchase_order_id => 996).where('sum(pallet_purchase_position_assignments.quantity) < purchase_positions.quantity').includes(:pallet_purchase_position_assignments)
我的模型:
class PurchasePosition < ActiveRecord::Base
has_many :pallet_purchase_position_assignments, :class_name => "PalletPurchasePositionAssignment"
has_many :pallets, :class_name => "Pallet", :through => :pallet_purchase_position_assignments
end
class Pallet < ActiveRecord::Base
has_many :pallet_purchase_position_assignments, :class_name => "PalletPurchasePositionAssignment"
has_many :purchase_positions, :class_name => "PurchasePosition", :through => :pallet_purchase_position_assignments
end
class PalletPurchasePositionAssignment < ActiveRecord::Base
belongs_to :pallet, :class_name => "Pallet", :foreign_key => "pallet_id"
belongs_to :purchase_position, :class_name => "PurchasePosition", :foreign_key => "purchase_position_id"
end
我得到的是一個MySQL錯誤
ActiveRecord::StatementInvalid: Mysql2::Error: Invalid use of group function
我是絕對不知道在哪裏我的錯誤在於: -/
難道有人有解決我的問題?
邁克爾
謝謝肖恩,這幫了很多;) – sufu90