0

我有以下代码:

def analytics 
    @user_registrations = User.count(:conditions => ["created_at >= ?", 30.days.ago.to_date], group: "date(created_at)")
    @daily_count = []
    30.times do |day|
      @daily_count << [Date.today - day, @user_registrations['#{Date.today - day}']]
    end
  end

当运行时:<%= simple_format @user_registrations.to_yaml %>产生:

--- !omap 
- '2012-08-23': 11

我想取出11值并将其放入我的 daily_count 数组中。

我怎样才能做到这一点?

更新:

<%= simple_format @user_registrations.to_yaml %>
<%= simple_format @daily_count.to_yaml %>

结果图像:

**在此处输入图片描述**

4

1 回答 1

1

只需更换:

@daily_count << [Date.today - day, @user_registrations['#{Date.today - day}']]

和:

@daily_count << [Date.today - day, @user_registrations["#{Date.today - day}"] || 0]

单引号将导致 #{} 转义语法被视为文字,双引号导致它被评估。

于 2012-08-23T16:45:13.887 回答