有没有办法使用groupdate gem和动态参数进行数据解析?
通常你可以使用
User.group_by_day(:created_at).count
或
User.group_by_week(:created_at).count
但我想使用参数的日/周选项来更改图表。所以我需要像
这样的东西User.group_by_groupdate(:created_at, resolution: params[:resolution]).count
params [:resolution]可以是" day"," week"," month"," year&#34 ;,而不是做像这样的东西:
case params[:resolution]
when "day"
User.group_by_day(:created_at).count
when "week"
User.group_by_week(:created_at).count
end
答案 0 :(得分:1)
如docs所述,您可以这样做
User.group_by_period(params[:resolution], :created_at, permit: %w[day week month year]).count
在permit
选项中,您可以传递可用的期间。当您传递其他内容时,将会引发ArgumentError
。