我编写了一个调度程序功能,可以自动检查用户的免费试用是否已经结束,然后自动将它们升级为付费订阅。问题是,当我尝试从命令行运行任务时,会发生以下错误:
没有方法错误:未定义的方法`find_each'对于用户:类
task :process_trial_accounts => :environment do
puts "Processing Trial Users"
User.find_each do |user|
plan_id = MailFunnelsUser.get_user_plan(user.client_id)
if plan_id == -99
products = Infusionsoft.data_query('SubscriptionPlan', 100, 0, {}, [:Id, :PlanPrice])
product = products.select { |product| product['Id'] == 2 }[0]
unless product
end
price = product['PlanPrice']
cardId = 0
current_year = Date.today.strftime('%Y')
current_month = Date.today.strftime('%m')
creditCardId = Infusionsoft.data_query('CreditCard',
100,
0,
{'ContactId' => user.clientid, 'ExpirationYear' => '~>=~' + current_year, 'Status' => 3},
[:Id, :ContactId, :ExpirationMonth, :ExpirationYear]
).each do |creditCard|
if Integer(creditCard['ExpirationYear']) == Integer(current_year)
if Integer(creditCard['ExpirationMonth']) >= Integer(current_month)
cardId = creditCard['Id']
end
else
cardId = creditCard['Id']
end
end
if cardId != 0
subscription_id = Infusionsoft.invoice_add_recurring_order(user.clientid, true, 2, 4, cardId, 0, 0)
invoice_id = Infusionsoft.invoice_create_invoice_for_recurring(subscription_id)
upgrade_response = Infusionsoft.invoice_charge_invoice(invoice_id, "Automatic Upgrade to 1000 Subscriber Tier", cardId, 4, false)
if upgrade_response[:Successful]
# Tag for 1000 sub tier level plan
new_tier_level_tag = 106
# Tags to remove from user
trial_ended_tag = 145
failed_payment_tag = 120
# Remove Tags from user for failed payment and Trial ended
Infusionsoft.contact_remove_from_group(user.clientid, trial_ended_tag)
Infusionsoft.contact_remove_from_group(user.clientid, failed_payment_tag)
# Add tag to user for 1000 subscribers tier level
Infusionsoft.contact_add_to_group(user.clientid, new_tier_level_tag)
end
end
else
next
end
end
end
我的应用程序托管在heroku上,我试图通过那里运行这项工作。任何帮助表示赞赏!谢谢!
答案 0 :(得分:0)
find_each
方法是ActiveRecord
方法,这意味着User类必须从ActiveRecord继承。如果您未使用此类ORM,则无法使用find_each
。
如果您确实提供了User
类代码库,那么会更容易。
干杯