显示视图中的记录总和 - Rails

时间:2013-12-17 11:01:44

标签: ruby-on-rails ruby activerecord sum

我有一个范围应该返回列的总和(下面的模型和范围)

class Member < ActiveRecord::Base
 belongs_to :membership
 accepts_nested_attributes_for :membership

 attr_accessible :membership_attributes, :forename, :middlename, :surname, :house_no, :house_name, :street, :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email, :start_date, :expiry_date

scope :new_memberships_cash_today, ->() {
joins(:membership).where(:start_date => Date.today).sum('memberships.cost')}
end

class Membership < ActiveRecord::Base
 has_many :members, :dependent => :destroy
 attr_accessible :membership_type, :cost 
end

当我在控制台中运行范围时,我得到了返回

<ActiveRecord::Relation [#<Member id: 26, forename: "Richard", middlename: "C", surname: "Lewis", house_no: 10, house_name: "", street: "testRoad", town: "test town", postcode: "cf5 6gh7GA", home_tel: "01633 222222", mobile_tel: "07855666995", work_tel: "02920 111111", email: "richlewis14@test.com", created_at: "2013-12-11 16:46:44", updated_at: "2013-12-17 10:12:26", membership_id: 3, start_date: "2013-12-17", expiry_date: "2014-01-16">]>

因此范围在start_date上匹配,我有一个membership_id,我应该能够从会员资格中访问属性成本吗?我似乎无法访问它,但在我看来

在视图中,我试图显示该总和

columns do

  #Todays Memberships
   column do
    panel "Todays Memberships", :class => 'todayscashmemberships' do
     table_for Member.new_memberships_cash_today do 
       column("Amount")  do |m| 
         "#{m.membership.cost}"
       end


     end
    end
   end
  end

我得到的错误是

undefined method `to_key' for 71.95999908447266:Float

有人能指出我做错了什么,也许可以解释以备将来参考

由于

1 个答案:

答案 0 :(得分:1)

我认为这个范围内的问题   new_memberships_cash_today返回71.95999908447266:Float并尝试迭代table_for并在float上调用membership.cost
尝试没有迭代只是面板或其他东西。
我不记得ActiveAdmin DSL单向或尝试阅读文档

columns do
  column do
     span Member.new_memberships_cash_today.to_s
  end
end