我有以下型号:
# == Schema Information
#
# Table name: quotes
#
# id :integer not null, primary key
# bound_rate_id :integer
class Quote < ActiveRecord::Base
#snip
end
# == Schema Information
#
# Table name: rates
#
# id :integer not null, primary key
# quoted_premium :integer
class Rate < ActiveRecord::Base
#snip
end
我想创建一个查询,该查询将计算与此循环相同的内容:
sum = 0
for quote in Quote.all
rate = Rate.find(quote.bound_rate_id)
sum += rate.quoted_premium
end
我将如何使用 ActiveRecord 的查询界面来做到这一点?(我正在使用 Rails 4。)
编辑:我已经有ActiveRecord
来自以前查询的实例Quote
,所以我希望我的查询从quotes
表开始并加入rates
表,而不是相反。像这样:
some_quotes = Quote.where(:some_other_property, my_param);
sum_of_rates = some_quotes.?????