mysql - Searchkick 使用 DateTime 查询的语法是什么

标签 mysql ruby-on-rails ruby elasticsearch searchkick

我正在使用gem searchkick我想做这样的查询:Product.where("products.created_at < products.distribution_ended_at")在searchkick的搜索方法中

这是我的模型产品

class Product < ActiveRecord::Base

searchkick word_start: [:name]

def search_data
    {
            name:           name,
            content:        content,
            amount:         amount,
            created_at:     created_at,
            distribution_ended_at: distribution_ended_at
    }


  end


end

这是我的产品 Controller

    class ProductsController < ApplicationController
    
        def index
    
            @products = Product.all
    
            search_conditions = {
                created_at: {lt: distribution_ended_at}
              }
    
            sort_conditions =[]
    
        aggregations = {}
    
        @product  = Product.new
        @products = Product.search("*", aggs: aggregations, where: search_conditions,
          page: params[:page], per_page: 20, match: :word_start, order: sort_conditions)

        end
end

但问题是,找不到 distribution_ended_at

有人知道如何在 Searchkick 的搜索条件中编写此查询吗?

谢谢

最佳答案

distribution_ending_at 可能等于 nil。尝试:

search_conditions = {
  created_at: { lt: params[:distribution_ended_at] }
}

关于mysql - Searchkick 使用 DateTime 查询的语法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42806027/

相关文章:

ruby-on-rails - Rails 正在寻找/config/config/database.yml

mysql - Pundit 通过联接表确定模型所有权范围 - Rails 4

Mysql 或/和优先级?

mysql - 如何从 MySQL 交叉表计算团队在一场比赛中得分最高的次数?

sql - 使用 MySQL 查询选择最接近的数值

ruby-on-rails - Rails/PostgreSQL - 如何验证只能有两个用户 ID 的一种组合?

ruby-on-rails - 在自定义位置/ View 中呈现 Rails 教程中的 'micropost feed'?

ruby - 能不能像函数式编程一样处理Ruby的方法,比如memoizing一个函数?

ruby - Ruby 中是否存在 C#'s ` #region` 关键字之类的东西

php - 可恢复的 fatal error : Object of class PDOStatement could not be converted to string in - index. php 第 23 行