我使用Groupdate Gem根据日期过滤我的对象,但似乎调用该函数的唯一方法是这样:
@foo = Model.group_by_hour(:created_at).count
@foo = Model.group_by_month(:created_at).count
@foo = Model.group_by_day(:created_at).count
...
理想情况下,我希望能够使用变量,以便用户可以选择是按月,年,日等进行过滤。
有没有办法在此函数中插入变量以允许更灵活的代码?
类似的东西:
@foo = Model."group_by_#{ time_variable }"(:created_at).count
答案 0 :(得分:1)
之类的东西?
@foo = Model.send("group_by_#{time_variable}", :created_at).count
或者宝石支持以下内容:
@foo = Model.group_by(time_variable => :created_at).count