我有:
scaffold product name price:float
scaffold client name email
scaffold event title product_id user_id
事件属于客户端和产品。如何在客户/展示中显示sum
价格?
Clients_controller:
@events = @client.events
客户端/显示:
- @events.each do |event|
= event.product.uprice
答案 0 :(得分:3)
在客户端模型中添加products
关联
def Client
has_many :events
has_many :products, through: events
end
所以你可以,
@total_price = @client.products.sum(:price)
阅读文档:http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
答案 1 :(得分:0)
完成!谢谢你,Shin。
解决方案:
1)在模型中
has_many :products, through: :fiscal_periods
2)在控制器中
@total_price = @user.products.sum(:uprice)
3)在节目中
= @total_price