[英]Update multiple records in the DB, one time per record in Rails
我有一個代碼正在更新數據庫中的用戶記錄,但正在更新每個用戶的記錄,即數據庫中有用戶的次數,例如,我在SQL中有3個用戶=> medic1,medic2,medic4可以看到它正在對它們進行3次更新,而每個用戶應該更新1次。
我的代碼應該做的是從數據庫中提取具有嵌套屬性period_end_date的用戶,並將其保存在哈希中,該哈希將查詢Openpay API的訂閱狀態; 在響應哈希中,我接收每個用戶的數據,並根據“狀態”,使用response_hash的結果更新數據庫中的值,該值應針對每個用戶。 我看不到我在做什么錯,您能幫我解決嗎?
以下是代碼rake premium_users:get_users(lib / tasks / premium_users.rake):
namespace :premium_users do
desc 'Get the user id & data in a hash'
task get_users: :environment do
users = Medic.includes(:payment_methods).where.not payment_methods: { period_end_date: nil }
request_hash = {}
# Get users in a hash
users.each do |user|
period_end_date = user.payment_methods.last.period_end_date
if Date.today - period_end_date <= 172_800
openpay_customer_id = user.payment_methods.last.openpay_customer_id
openpay_subscription_id = user.payment_methods.last.openpay_subscription_id
medic_id = user.payment_methods.last.medic_id
user_hash = {}
user_hash[medic_id] = [openpay_customer_id, openpay_subscription_id]
else
user_hash = {}
end
request_hash.merge!(user_hash) # If no users is empty => {}
if request_hash.empty?
puts 'No User to check!'
else
# Request for subscription status to Openpay
request_hash.each_value do |value|
openpay_customer_id = value[0]
openpay_subscription_id = value[1]
# Openpay API connection
@openpay = OpenpayApi.new(ENV['MERCHANT_ID_OPENPAY'], ENV['SECRET_KEY_OPENPAY'])
@subscriptions = @openpay.create(:subscriptions)
response_hash = @subscriptions.get(openpay_subscription_id, openpay_customer_id)
# Extract values from response
@charge_date, @creation_date, @current_period_number, @period_end_date, @status, @trial_end_date = response_hash.values_at('charge_date', 'creation_date', 'current_period_number', 'period_end_date', 'status', 'trial_end_date')
case @status
when 'past_due'
@premium = false
@cancelled = Time.now
when 'unpaid'
@premium = false
@cancelled = Time.now
when 'cancelled'
@premium = false
@cancelled = Time.now
else
@premium = true
@cancelled = nil
end
end
# Update user payment record with response from Openpay
@payment_method = PaymentMethod.update(
premium: @premium,
charge_date: @charge_date,
creation_date: @creation_date,
current_period_number: @current_period_number,
period_end_date: @period_end_date,
status: @status,
trial_end_date: @trial_end_date,
cancelled_on: @cancelled
)
puts "User #{user.email} update, Complete!"
end
end
end
end
SQL:
PaymentMethod Load (0.4ms) SELECT `payment_methods`.* FROM `payment_methods`
(0.1ms) BEGIN
SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:15:13', `updated_at` = '2018-06-15 21:01:19' WHERE `payment_methods`.`id` = 1
(11.9ms) COMMIT
(0.1ms) BEGIN
SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:15:13', `updated_at` = '2018-06-15 21:01:19' WHERE `payment_methods`.`id` = 2
(2.7ms) COMMIT
(0.1ms) BEGIN
SQL (0.2ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:15:13', `updated_at` = '2018-06-15 21:01:19' WHERE `payment_methods`.`id` = 3
(2.6ms) COMMIT
User medic1@mail.com update, Complete!
PaymentMethod Load (0.3ms) SELECT `payment_methods`.* FROM `payment_methods`
(0.1ms) BEGIN
SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:16:24', `updated_at` = '2018-06-15 21:01:20' WHERE `payment_methods`.`id` = 1
(11.9ms) COMMIT
(0.1ms) BEGIN
SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:16:24', `updated_at` = '2018-06-15 21:01:20' WHERE `payment_methods`.`id` = 2
(2.7ms) COMMIT
(0.1ms) BEGIN
SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:16:24', `updated_at` = '2018-06-15 21:01:20' WHERE `payment_methods`.`id` = 3
(2.5ms) COMMIT
User medic2@mail.com update, Complete!
PaymentMethod Load (0.3ms) SELECT `payment_methods`.* FROM `payment_methods`
(0.1ms) BEGIN
SQL (0.2ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:18:30', `updated_at` = '2018-06-15 21:01:22' WHERE `payment_methods`.`id` = 1
(2.6ms) COMMIT
(0.1ms) BEGIN
SQL (0.2ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:18:30', `updated_at` = '2018-06-15 21:01:22' WHERE `payment_methods`.`id` = 2
(2.6ms) COMMIT
(0.1ms) BEGIN
SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:18:30', `updated_at` = '2018-06-15 21:01:22' WHERE `payment_methods`.`id` = 3
(2.6ms) COMMIT
User medic4@mail.com update, Complete!
楷模:
class Medic < ApplicationRecord
has_many :payment_methods, dependent: :destroy
end
class PaymentMethod < ApplicationRecord
belongs_to :medic, optional: true
end
感謝您的回答,他們終於幫助我意識到我必須更新每個對象,因此我通過PaymentMethod.where(id: @subscription_id, medic_id: @medic_id).update()
做到了這一點。
namespace :premium_users do
desc 'Get the user id & data in a hash'
task get_users: :environment do
# Get the user and association payment_method where period_end_date not nil, means that this user has a payment_method
users = Medic.includes(:payment_methods).where.not payment_methods: { period_end_date: nil }
request_hash = {}
users.each do |user|
# Set to compare period end date with date today
period_end_date = user.payment_methods.last.period_end_date
# Create a user hash if period_end_date is 2 days from date today
if Date.today - period_end_date <= 172_800
medic_id, subscription_id, openpay_customer_id, openpay_subscription_id = user.payment_methods.last.medic_id, user.payment_methods.last.id, user.payment_methods.last.openpay_customer_id, user.payment_methods.last.openpay_subscription_id
user_hash = {}
user_hash[medic_id] = [subscription_id, openpay_customer_id, openpay_subscription_id]
else
user_hash = {}
end
request_hash.merge!(user_hash) # If no users is empty => {}
if request_hash.empty?
puts 'No User to check!'
else
request_hash.each do |key, value|
@medic_id, @subscription_id, openpay_customer_id, openpay_subscription_id = key, value[0], value[1], value[2]
# Openpay API connection and request
@openpay = OpenpayApi.new(ENV['MERCHANT_ID_OPENPAY'], ENV['SECRET_KEY_OPENPAY'])
@subscriptions = @openpay.create(:subscriptions)
# @subscriptions.get(subscription_id,customer_id)
response_hash = @subscriptions.get(openpay_subscription_id, openpay_customer_id)
# Split values
@charge_date, @creation_date, @current_period_number, @period_end_date, @status, @trial_end_date = response_hash.values_at('charge_date', 'creation_date', 'current_period_number', 'period_end_date', 'status', 'trial_end_date')
# Status conditions
case @status
when 'past_due'
@premium = false
@cancelled = Time.now
when 'unpaid'
@premium = false
@cancelled = Time.now
when 'cancelled'
@premium = false
@cancelled = Time.now
else
@premium = true
@cancelled = nil
end
end
# Update each subscription with data from API response
@payment_method = PaymentMethod.where(id: @subscription_id, medic_id: @medic_id).update(
premium: @premium,
charge_date: @charge_date,
creation_date: @creation_date,
current_period_number: @current_period_number,
period_end_date: @period_end_date,
status: @status,
trial_end_date: @trial_end_date,
cancelled_on: @cancelled
)
puts "User #{@medic_id} update, Complete!"
end
end
end
end
結果:
PaymentMethod Load (0.5ms) SELECT `payment_methods`.* FROM `payment_methods` WHERE `payment_methods`.`id` = 1 AND `payment_methods`.`medic_id` = 1
(0.2ms) BEGIN
SQL (0.4ms) UPDATE `payment_methods` SET `premium` = 1, `current_period_number` = 0, `status` = 'trial', `updated_at` = '2018-06-20 17:40:20' WHERE `payment_methods`.`id` = 1
(12.1ms) COMMIT
User 1 update, Complete!
PaymentMethod Load (0.5ms) SELECT `payment_methods`.* FROM `payment_methods` WHERE `payment_methods`.`id` = 2 AND `payment_methods`.`medic_id` = 3
(0.2ms) BEGIN
SQL (0.4ms) UPDATE `payment_methods` SET `premium` = 1, `status` = 'trial', `cancelled_on` = NULL, `updated_at` = '2018-06-20 17:40:22' WHERE `payment_methods`.`id` = 2
(11.8ms) COMMIT
User 3 update, Complete!
0.040000 0.010000 0.050000 ( 1.741126)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.