Searchlogic doesn't convert the time properly for datetime conditions

怎甘沉沦 提交于 2019-12-11 14:03:28

问题


The author of Searchlogic says that it is delegated to A::R converter, but at least in our case this didn't cover the usual cases. Local time was 'interpreted' as UTC and therefore was moved by one hour (CET).

How can I do that properly?

I add our current workaround as an answer, hopefully it helps somebody!


回答1:


We've added the following method to the application controller:

  protected
  def parse_datetime_fields(hash, key)
    value = hash[key]
    return unless value
    hash[key] = Time.zone.parse(value)
  end

And then before creating the searchlogic object we 'preprocess' the params hash:

if params[:search]
  parse_datetime_fields(params[:search], :begin_greater_than)
  parse_datetime_fields(params[:search], :begin_less_than)
end

@search = Record.search(params[:search])

Any clearer better and nicer solutions/ideas are very appreciated :)!

our environment.rb:

  config.time_zone = 'Bern'
  config.active_record.default_timezone = :utc


来源:https://stackoverflow.com/questions/2280980/searchlogic-doesnt-convert-the-time-properly-for-datetime-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!