我有以下关系:
class Order < ActiveRecord::Base
has_many :item_selections, :dependent => :destroy
has_many :inventory_items, :through => :item_selections
end
class InventoryItem < ActiveRecord::Base
has_many :item_selections, :dependent => :destroy
has_many :orders, :through => :item_selections
end
class ItemSelection < ActiveRecord::Base
belongs_to :order
belongs_to :inventory_item
end
我正在尝试在下面创建此 SQL 查询的 ActiveRecord 等效项,然后将 *total_weight* 和 *total_volume* 列的总和加载到实例变量中:
select t1.quantity, t2.volume, t2.weight,
t2.volume * t1.quantity as total_volume,
t1.quantity * t2.weight as total_weight
from orders t0
inner join item_selections t1 on t0.id = t1.order_id
inner join inventory_items t2 on t1.inventory_item_id = t2.id
where t0.id = <id_val>
关于使用 ActiveRecord 获取这些值的正确方法的任何想法?