Rails ransack和paper_trail在postgresql json列中搜索密钥

时间:2017-04-28 20:21:40

标签: ruby-on-rails ransack paper-trail-gem

我使用Ruby on Rails创建我的网站。我使用paper_trail gem来保存我的模型和ransack 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密钥是whit - &gt;&gt;然后正确的方法是:

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

我是怎么做到的?

1 个答案:

答案 0 :(得分:0)

我遇到了像您这样的问题,并通过添加以下内容解决了该问题:

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