简体   繁体   中英

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)

filter :project_event_date, label: 'By Project Event Date', as: :date_range

Project.rb(model)

 has_many :project_events, dependent: :destroy

 ransacker :project_event_date do
  "project_events.event_date"
 end

According to the docs , 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)

A String is not a Date . So, convert your String to a Date and you should be good to go.

首先将字符串转换为日期,然后应用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

active_admin has custom formatter for predicate like
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

在数据源中将日期时间转换为日期字段为我解决了问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM