Rails 搜索并 paper_trail 在 postgresql json 列中搜索键

Rails ransack and paper_trail search for a key in a postgresql json column

我在 Rails 上使用 Ruby 创建我的网站。我正在使用 paper_trail gem 来保存我的模型的历史更改,并使用搜索 gem 来执行搜索表单。一切正常,但我想创建一个搜索表单来过滤对我的模型所做的更改。

这是erb过滤的代码框架:

  <%= search_form_for [@q, @q_deleted], url: ticket_index_path, class: 'form-inline' do |f| %>
    <div class="row">
      <div class="col-sm-6 col-lg-4">
        <%= f.label :versions_whodunnit_eq, t('audit.view.text.user').capitalize, {class: "control-label"} %>
        <%= f.select :versions_whodunnit_eq, User.all.collect { |p| [p.name, p.id] }, {include_blank: true}, {class: 'form-control input-sm'} %>
      </div>
      <div class="col-sm-6 col-lg-4">
        <%= f.label :versions_event_eq, t('audit.view.text.event').capitalize, {class: "control-label"} %>
        <%= f.select :versions_event_eq, {t('common.versions_event.create') => 'create', t('common.versions_event.update') => 'update', t('common.versions_event.destroy') => 'destroy'}, {include_blank: true}, {class: 'form-control input-sm'}  %>
      </div>
      <div class="col-sm-6 col-lg-4">
        <%= f.label :versions_object_changes_eq, t('audit.view.text.change').capitalize, {class: "control-label"} %>
        <%= f.select :versions_object_changes_eq, Audit::TicketsController::COLUMNS.collect { |column| [t("audit.view.text.column_change.#{column}"), column] }, {include_blank: true}, {class: 'form-control input-sm'} %>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-6 col-lg-3">
        <br>
        <%= f.submit 'Buscar', class: 'btn btn-primary' %>
        <%= link_to 'Limpiar', request.path, class: 'btn btn-default cancel-button' %>
      </div>
    </div>
  <% end %>

并进入视图控制器:

params[:q] ||= {}

params[:page] = 1 if params[:page].blank?
params[:page_deleted] = 1 if params[:page_deleted].blank?

@q = Ticket.all.order(date: :desc).ransack(params[:q])
@q_deleted = Ticket.only_deleted.order(date: :desc).ransack(params[:q])

result = @q.result(distinct: true)
result_deleted = @q_deleted.result(distinct: true)

@active_count = result.count
@deleted_count = result_deleted.count

@tickets = result.paginate(page: params[:page], per_page: params[:per_page])
@tickets_deleted = result_deleted.paginate(page: params[:page_deleted], per_page: params[:per_page])

问题是,当我按 :versions_object_changes_eq 进行过滤时,PostgreSQL 会引发一个错误,该错误可以比较 json 列:

SELECT DISTINCT "tickets".* FROM "tickets" LEFT OUTER JOIN "versions" ON "versions"."item_id" = "tickets"."id" AND "versions"."item_type" = 'Ticket' WHERE "tickets"."deleted_at" IS NULL AND "versions"."object_changes" = 'date' ORDER BY "tickets"."date" DESC

SQL 的问题是访问 json 键是白色 ->> 然后正确的方法是:

SELECT DISTINCT "tickets".* FROM "tickets" LEFT OUTER JOIN "versions" ON "versions"."item_id" = "tickets"."id" AND "versions"."item_type" = 'Ticket' WHERE "tickets"."deleted_at" IS NULL AND ("versions"."object_changes"->>'ticket_state' IS NOT NULL) ORDER BY "tickets"."date" DESC

我该怎么做?

.. when I filter by :versions_object_changes_eq [I get this SQL]

where "versions"."object_changes" = 'date'

[but I want:]

where "versions"."object_changes"->>'ticket_state' IS NOT NULL

我认为 ransack 不支持 ->> 运算符。你可以尝试写一个custom ransacker,但是他们没有很好的文档,所以我不推荐它。

PaperTrail 支持 ->> 运算符。使用 PaperTrail::Version.where_object。但是,这可能很难与 Ticket.ransack 结合起来。

以下是您可以尝试的三件事:

  1. 使用ActiveRecord的merge方法合并Ticket.ransackVersion.where_object
  2. 执行两个单独的查询并获取其结果的集交集
  3. 有条件地将您想要的确切原始 SQL 添加到您的 Relation

我遇到了和你一样的问题,并通过添加如下内容解决了它:

ransacker :ticket_state do |parent|
  Arel::Nodes::InfixOperation.new('->>', parent.table[:object_changes], Arel::Nodes.build_quoted('ticket_state'))
end

给我的模特。然后你可以在 ticket_state 上使用任何 Ransack 谓词,例如:

Ticket.ticket_state_not_null