Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何将一年中的所有每日数据转换为每月数据_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 如何将一年中的所有每日数据转换为每月数据

Ruby on rails 如何将一年中的所有每日数据转换为每月数据,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,首先,让我告诉你我想要什么,我想制作一张用户共同基金投资组合增长的图表 现在我有一个用户去年投资了一个投资组合 现在我有一个任务,它存储用户的所有每日增长值 用户组合增长表中的组合 现在,截至当前日期,我的用户投资组合增长表中有投资组合增长的所有每日值 我想在图表上显示用户组合的每月增长 所以,我这里的问题是,如何从他的投资日期开始,将所有的每日价值转换为每月价值?所以,我可以在图表上显示它 用户组合模式 # == Schema Information # # Table name: user_

首先,让我告诉你我想要什么,我想制作一张用户共同基金投资组合增长的图表

现在我有一个用户去年投资了一个投资组合

现在我有一个任务,它存储用户的所有每日增长值 用户组合增长表中的组合

现在,截至当前日期,我的用户投资组合增长表中有投资组合增长的所有每日值

我想在图表上显示用户组合的每月增长

所以,我这里的问题是,如何从他的投资日期开始,将所有的每日价值转换为每月价值?所以,我可以在图表上显示它

用户组合模式

# == Schema Information
#
# Table name: user_portfolios
#
#  id              :integer          not null, primary key
#  user_id         :integer          not null
#  portfolio_name  :string
#  amount          :integer          not null
#  investment_plan :string           not null
#  duration        :integer          default(1), not null
#  risk_level      :string           not null
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
UserPortfolioGrowth

# == Schema Information
#
# Table name: user_portfolio_growths
#
#  id                :integer          not null, primary key
#  user_portfolio_id :integer
#  appreciated_value :float
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#
谢谢大家!

编辑: 锡人问我做了什么: 这是我到目前为止尝试过的演示

最初我尝试了这种类型的代码:

start = 10.months.ago.to_date
end_date = Date.today
(start..end_date).each do |date|
  puts date.end_of_month
end 
我得到了一个庞大的结束日期列表,因为它覆盖了所有日期:

2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
............. and so on...
但我想要的只是一个月最后一天的估计值,所以基本上如果我迭代10个月,我想要的就是10个值。 因此,我试图通过以下逻辑将指数提前30天增加

start = 10.months.ago.to_date
end_date = Date.today
(start..end_date).each do |date|
  puts date.end_of_month
  date = date.end_of_month + 1.day
end
但我仍然得到以下输出:

2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
2015-07-31
........................... and so on
我认为增加索引值可能会改变结果,但事实并非如此。 有人能提出解决这个问题的建议吗

编辑: 我想我找到了答案,我用while循环检查
end\u date

这就是我所做的:

 start = 10.months.ago.to_date
    end_date = Date.today
    date_i = start
    while(date_i < end_date)
      puts date_i.end_of_month
      date_i = date_i + 1.month
    end

你要做的是,你必须计算出一个月的所有金额或投资价值,或者你想得到的任何东西

现在把它们加起来

取平均数

我们取本月的工资

total_amount = UserPortfolio.where(created_at: (Date.today.beginning_of_month..Date.today.end_of_month)).pluck(:amount)
average_amount = total_amount.inject(0, :+)/total_amount.count
这将为您提供当月的平均值 同样地,你也可以在一年中的每一个月都得到

然后将其放入散列或数组中,并在图形中显示它们。

尝试:

> months_result = user_portfolio.user_portfolio_growths.group_by{|e| e.created_at.strftime('%B')}
> month_with_total = months_result.each{|month, data| [month, data.pluck(:appreciated_value).sum]}.to_h
#=> {"January" => 12546.00, "February" => 56487.00} something like this

编写sql不是一种选择吗?请提供您的表和模型结构。完成,即使我得到了从用户投资日期到当前数据的每个月的最后值,也很好。建议的答案是可以的,但您应该让DB为您的客户完成这项工作。这样可以节省资源。你在用什么数据库?我看到很多“我想要什么”和“现在”,没有显示你的尝试或努力的证据。请阅读这篇文章,它只给出当前月份的结果。实际上我不想要平均值,但想要一个月的结束日期。然后用(date.today.end\u of\u month)代替(date.today.start\u of\u month..date.today.end\u of\u month)hi@PraveshKhatri我遇到的问题是,我们正在迭代日期,即开始日期到结束日期,因此使用开始日期给出从开始日期到结束日期的所有日期。结束日期,例如,我从60.days.ago开始到日期。今天,它给出所有60个月的开始日期,但我需要2个值,即两个日期。如果你想要,只有开始日期和结束日期,然后你就可以得到它了,上面的查询只是给你整个范围:
UserPortfolio.where(创建时间:(date.today.start\u of\u month))
UserPortfolio.where(创建时间:(date.today.end\u of\u month))
而不是扔掉代码,解释代码为什么有用以及它的作用。这个想法是以一种教育的方式来回答,而不仅仅是解决眼前的问题。
> months_result = user_portfolio.user_portfolio_growths.group_by{|e| e.created_at.strftime('%B')}
> month_with_total = months_result.each{|month, data| [month, data.pluck(:appreciated_value).sum]}.to_h
#=> {"January" => 12546.00, "February" => 56487.00} something like this