ruby on rails计算表值的总和

时间:2016-02-17 11:06:12

标签: ruby-on-rails ruby

如何计算我定义的变量的总和?

我有:

product_sale.rb

def totalprice
  price*quantity
end

视图

%h1 Listing Product Sales
%table
  %thead
    %tr
      %th Product name
      %th Price
      %th Quantity
      %th Total price
  %tbody
    - @product_sales.each do |product_sale|
      %tr
        %td= product_sale.product_name
        %td= product_sale.price
        %td= product_sale.quantity
        %td= product_sale.totalprice
%br
%h1
  Total sum to be paid:
  = Product_sale.sum(:totalprice)

= Product_sale.sum(:totalprice)不起作用。我怎么计算呢?

5 个答案:

答案 0 :(得分:0)

 NavigationView navigationView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
        //here will scroll to top
((NavigationMenuView)navigationView.getChildAt(0)).smoothScrollToPosition(0);

        return true;
    }

答案 1 :(得分:0)

@product_sales.sum('price * quantity') # single query, efficient

P.S。该类的正确名称是snakecased ProductSale

答案 2 :(得分:0)

@product_sales.sum(&:totalprice)

我认为这应该适用于您只选择所选产品销售总价的

答案 3 :(得分:0)

@product_sales.sum('price * quantity')

答案 4 :(得分:0)

对我来说最好的解决方案是Max:

控制器索引视图:library(ggplot2) ggplot(df_clean, aes(x = Factor, y = measure, fill = Factor)) + geom_bar(stat = "identity") + geom_errorbar(aes(ymin = measure - SD, ymax = measure + SD)) + facet_wrap(~ measure_letter)

查看:@some_total = @product_sales.map(&:totalprice).sum