The*_*o B 5 ruby-on-rails meta-search activeadmin
我有一个带有:birthday属性的Model,我希望按表单中指定的月份过滤掉这些日期(ActiveAdmin的DSL:select).
这是仅提取生日日期的简单范围的示例.也许可以帮助:
scope :birthday_month, where('extract(month from birthday) = ?', Date.today.month)
Run Code Online (Sandbox Code Playgroud)
这是一个解决方案:
在我的Active Admin资源(app/admin/employees.rb)上,我放了:
filter :month, :as => :select, :collection => (1..12)
Run Code Online (Sandbox Code Playgroud)
在我的模型(app/models/employee.rb)上:
scope :month_eq, lambda{ |month| where("strftime('%m', birthday) + 0 = ?", month.to_i) }
search_methods :month_eq
Run Code Online (Sandbox Code Playgroud)
这种方法定义'_eq'范围方法(MetaSearch),并通过search_methods :month_eq(MetaSearch)将其转换为可用范围.
注意:
如果您不使用sqlite,也许您希望使用它where('extract(month from birthday) = ?', month.to_i).