代码之家  ›  专栏  ›  技术社区  ›  rmagnum2002

将ActiveRecord查询转换为PostgreSQL查询

  •  1
  • rmagnum2002  · 技术社区  · 6 年前

    ValidationEmergency.with_deleted.where(
      code: 'aml_validation_failed', 
      service_name: 'ComplyAdvantage', service_type: nil).each do |e|
        e.update_attributes(
          {
            service_type: 'Screening',
            service_id:   e.message.split(', ').last.split(' => ').last.to_i
          }, without_protection: true
        )
      end
    

    目前为止我所拥有的(在@fanta的评论之后编辑)

    ActiveRecord::Base.connection.execute("
      UPDATE validation_emergencies 
      SET 
        service_type = 'Screening', 
        service_id = (
          CAST ( array_upper(string_to_array(message, ' => '), 1) AS INTEGER )
        ) 
      WHERE 
        code = 'aml_validation_failed' 
      AND 
        service_name = 'ComplyAdvantage' 
      AND 
        service_type IS NULL"
    )
    

    请告知。谢谢您。

    1 回复  |  直到 6 年前
        1
  •  0
  •   rmagnum2002    6 年前

    ActiveRecord::Base.connection.execute(
      "UPDATE validation_emergencies
       SET
         service_type = 'Screening',
         service_id =
           CAST ((string_to_array(message, ' => '))[array_upper(string_to_array(message, ' => '), 1)] AS INTEGER)
       WHERE
         code = 'aml_validation_failed'
       AND
         service_name = 'ComplyAdvantage'
       AND
         service_type IS NULL"
    )