[英]undefined method beginning_of_day for string with ransacker custom filter
I'm getting below mentioned error. 我得到下面提到的错误。 any idea how i can fix it ? 知道我该如何解决吗?
undefined method `beginning_of_day' for "2018-01-01":String
/lib/ransack_ext.rb
config.add_predicate old, arel_predicate: current
end
config.add_predicate 'gteq_datetime',
arel_predicate: 'gteq',
formatter: ->(v) { v.beginning_of_day }
Code: 码:
Projects.rb (ActiveAdmin) Projects.rb(ActiveAdmin)
filter :project_event_date, label: 'By Project Event Date', as: :date_range
Project.rb(model) Project.rb(模型)
has_many :project_events, dependent: :destroy
ransacker :project_event_date do
"project_events.event_date"
end
According to the docs , beginning_of_day()
: 根据文档 , beginning_of_day()
:
Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) 将日期部分设置为一天的开始(0:00),将日期转换为时间(或日期时间,如有必要)
A String
is not a Date
. String
不是Date
。 So, convert your String
to a Date
and you should be good to go. 因此,将您的String
转换为Date
,您应该就可以了。
首先将字符串转换为日期,然后应用begin_of_day这样的方法:-
Date.parse("2018-01-01").beginning_of_day
this will help you 这会帮助你
ruby ransacker :project_event_date, type: :date do Arel.sql("project_events.event_date") end
ransacker block result should be built using Arel
like it shown on Using Ransacker Guide 应该使用Arel
构建ransacker块结果,如《 使用Ransacker指南》中所示
active_admin has custom formatter for predicate like active_admin具有用于谓词的自定义格式化程序,例如
formatter: proc { |v| v.beginning_of_day }
so you need to convert passed value into date 所以您需要将传递的值转换为日期
best way will be to provide type
or formatter
to ransacker 最好的方法是向ransacker提供type
或formatter
在数据源中将日期时间转换为日期字段为我解决了问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.