【问题标题】:How to pull record for this month and last month in rails?如何在rails中提取本月和上个月的记录?
【发布时间】:2016-04-07 17:08:35
【问题描述】:

我想把这个月和上个月的所有记录都拉出来,转换成百分比

@posts.all(本月).convert_to_% 和 @posts.all(上个月).covert_to_%

【问题讨论】:

  • 查看 groupdate gem github.com/ankane/groupdate
  • 不同的数据库让你以稍微不同的方式做到这一点。例如,使用 postgres 您可以执行类似 `Model.where("MONTH(date_column) = ? and Year(date_column) = ?", (DateTime.now -1.month).month, (DateTime.now -1.month ).年)

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord rails-activerecord


【解决方案1】:

您从数据库中执行此操作,但如果您只是想要一些有效的东西,请编写一个循环遍历每条记录的 foreach 语句,如果日期超出您的条件,则跳过该记录。

例如,这里是我针对 csv 文件使用的 foreach 循环的示例,该文件将推销员姓名与项目状态和日期进行比较。

def pending_sales(initials, salesperson)
      my_status = 'Pending'
      pending = 0
      count = 0
      CSV.foreach(path, :encoding => 'windows-1251:utf-8') do |row|
        salesman = row[1]
        project_manager = row[0]
        project_name = row[2]
        project_city = row[3]
        customer = row[4]
        status = row[6]
        bid_date = Date.strptime(row[7], '%m/%d/%Y')
        amount = row[8].tr(',', '').to_i
        next if salesman != initials || status != my_status || bid_date < fiscal_start
        dollar_amount = '$' + amount.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
        pending = pending + amount
        count = count + 1
      end
      @pending_count = count.to_s
      @pending_amount = "$" + pending.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
    end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多