从Rails中的对象Hash中获取价值

时间:2012-08-23 16:19:18

标签: ruby-on-rails ruby ruby-on-rails-3

我有以下代码:

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 %>

产生图像:

**enter image description here**

1 个答案:

答案 0 :(得分:1)

只需替换:

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

使用:

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

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