【问题标题】:Filter out the results based on audited changes with rails audited gem使用 rails audited gem 根据已审核的更改过滤掉结果
【发布时间】:2017-09-10 08:52:39
【问题描述】:

我正在使用经过审核的 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 包含名字的更改。这个怎么查询?

【问题讨论】:

    标签: ruby-on-rails acts-as-audited


    【解决方案1】:

    如果您使用的是 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}") # 对于所有Consumer 类型的记录

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

    【讨论】:

    • 嘿,这似乎不起作用。它只是返回一个活动记录关系..我正在使用 postgress
    • 您是否将列类型更改为 json 因为它对我有用stackoverflow.com/questions/23911740/…
    • audited_change 使用旧的 String YAML 数据类型时,这将起作用:.where("audited_changes LIKE '%first_name:%'")
    猜你喜欢
    • 2017-02-16
    • 1970-01-01
    • 2017-05-09
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2014-12-08
    • 1970-01-01
    相关资源
    最近更新 更多