1

我正在使用经过审核的 gem 来跟踪记录的更改。审计对象看起来像

  id: 1,
  auditable_id: 1,
  auditable_type: "Customer",
  associated_id: nil,
  associated_type: nil,
  user_id: nil,
  user_type: nil,
  username: nil,
  action: "create",
  audited_changes:
    {"first_name"=>"Milan McClure",
     "last_name"=>"Wilfred Carter IV",
     "email"=>"brian.hahn@vonrueden.com",
     "sms_optin"=>true,
     "title"=>nil,
     "is_deleted"=>false,
     "deleted_at"=>nil,
     "restored_at"=>nil},
  version: 1,
  comment: nil,
  remote_address: nil,
  request_uuid: "6e462bf8-4788-4ec5-a6ee-f31a7a8226d0",
  created_at: Wed, 12 Apr 2017 12:20:39 UTC +00:00>,

等等。现在我只想过滤掉那些审计,其中审计对象中的哈希 audit.audited_changes 包含名字的更改。我如何查询这个?

4

1 回答 1

2

如果您使用的是 PostgreSQL,并且可以将此 audited_changes 设置为 json 类型,则可以将查询触发为

Audited::Audit.where("(audited_changes->'first_name') is not null AND auditable_type = ?", "Consumer") # 对于 Consumer 类型的所有记录

或针对特定对象 Audited::Audit.where("(audited_changes->'first_name') is not null AND auditable_type = ? AND auditable_id = ?", "Consumer", "#{consumer_id}") # 对于所有记录消费者类型

因为无论何时创建审计对象,audited_changes 都只有您在创建/更新时更改的那些键。

于 2017-04-14T06:33:19.110 回答