将 ActiveRecord 查询转换为 PostgreSQL 查询
Convert ActiveRecord query to PostgreSQL query
我正在尝试将这段 rails 代码更新为原始 PostgreSQL
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"
)
服务类型正在正确更新,但 service_id 没有,仍在研究如何使用 postgresql 正确拆分和 gsub。
请指教。谢谢。
最终版本,对于service_id,它将拆分消息,获取最后一个元素并将其转换为整数。
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"
)
我正在尝试将这段 rails 代码更新为原始 PostgreSQL
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"
)
服务类型正在正确更新,但 service_id 没有,仍在研究如何使用 postgresql 正确拆分和 gsub。
请指教。谢谢。
最终版本,对于service_id,它将拆分消息,获取最后一个元素并将其转换为整数。
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"
)