相关疑难解决方法(0)

undefined方法`group_by_day' - rails 3.2

我想使用chartkick制作一个用户注册数量(每天)的折线图,这是一个简单的一行代码,但它给出了一个错误

`undefined method `group_by_day' for #<Class:0x00000004b4fbf0>`
Run Code Online (Sandbox Code Playgroud)

在我看来,我有

<%= line_chart User.group_by_day(:created_at).count %>
Run Code Online (Sandbox Code Playgroud)

错误日志:

Rendered admin/dashboards/index.html.erb within layouts/application (145.2ms)
Completed 500 Internal Server Error in 1018ms

NoMethodError - undefined method `group_by_day' for #<Class:0x00000004b4fbf0>:
  activerecord (3.2.14) lib/active_record/dynamic_matchers.rb:55:in `method_missing'
  app/views/admin/dashboards/index.html.erb:38:in `_app_views_admin_dashboards_index_html_erb___3330517857505238097_39214860'
  actionpack (3.2.14) lib/action_view/template.rb:145:in `block in render'
  activesupport (3.2.14) lib/active_support/notifications.rb:125:in `instrument'
  actionpack (3.2.14) lib/action_view/template.rb:143:in `render'
  actionpack (3.2.14) lib/action_view/renderer/template_renderer.rb:48:in `block (2 levels) in render_template'
  actionpack (3.2.14) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
  activesupport (3.2.14) lib/active_support/notifications.rb:123:in `block in instrument'
  activesupport (3.2.14) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails rails-activerecord

3
推荐指数
1
解决办法
3868
查看次数

如何在Postgres的Rails中按天分组和计数?

这个问题如何按日而不是日期分组?展示了如何在Ruby中进行分组.

这可行,但对于大量记录来说会非常慢.如何做到这一点,以便:created_at在Postgres内按日进行分组和计数?

sql postgresql activerecord ruby-on-rails ruby-on-rails-4

2
推荐指数
1
解决办法
2042
查看次数

如何使用 Postgres 在 Rails 中按天对记录进行分组

所以我发现了这些问题:

还有这个宝石: https: //github.com/ankane/groupdate

我希望按天对记录进行分组,格式如下:

[
  { "2016-03-16" => [Record1, Record2, Record3] },
  { "2016-03-17" => [Record1, Record2] },
  { "2016-03-18" => [Obj1, Obj2] }
]
Run Code Online (Sandbox Code Playgroud)

我已经能够使用以下代码获得这种格式:

def group_by_criteria
  created_at.to_date.to_s
end

list.group_by(&:group_by_criteria).map {|k,v| { k => v} }
Run Code Online (Sandbox Code Playgroud)

但是,正如其他问题中所解释的,这对于大量记录来说不是很有效,我认为我应该使用数据库中的分组,但我不确定如何获取我正在寻找的格式,我尝试过像这样的东西,但我没有得到我所期望的:

list.order("date_trunc('day', created_at) DESC).map{ |k, v| { k => v }}
Run Code Online (Sandbox Code Playgroud)

如何像这样按天对数据库中的记录进行分组?

postgresql ruby-on-rails

1
推荐指数
1
解决办法
942
查看次数