user 类:
class Delta::User < ActiveRecord::Base
has_many :orders, dependent: :destroy
order 类:
class Delta::Order < ActiveRecord::Base
belongs_to :user
has_one "product"
测试数据:
user1=Delta::User.new
user1.id=1
user1.username="tom"
p1=Delta::Product.new
p1.price=100
p1.id=1
p2=Delta::Product.new
p2.price=200
p2.id=2
order1=Delta::Order.new
order1.user_id=1
order1.product_id=2
order1.num=2
puts "==="
puts Delta::Order.vip_total_consumption
vip_total_consumption 代码如下:
def self.vip_total_consumption
total = 0.0
Delta::User.where(username: 'tom').each do |us|
puts us.orders.length
us.orders.each do |order|
total += order.num*order.product.price
end
end
return total
end
但是 puts us.orders.length 这句话没有打印出 数量 1 而是 0 请问哪里的问题???