【发布时间】:2017-01-05 01:02:25
【问题描述】:
我正在尝试创建一个 Rake 任务,该任务将在一个月内未更新列表但似乎无法正常工作时发送电子邮件。
我认为问题可能是我正在尝试提取 listing_agent 电子邮件,但其中有 listing_agent_id。
update_listing_mailer.rb:
class UpdateListingMailer < ActionMailer::Base
default from: "Help <help@realestate.com>"
def listing_update_agent(agent)
@agent = agent
@listing = listing
mail to: "#{agent.email}", subject: "Your listing hasn't been updated in a month!"
end
end
listing_update_agent.html.erb:
Hiya, <br><br>
The listings below have not been updated in a month! Is it still on the market? If so, please update. If not, please remove from the market.<br><br>
<strong>Address:</strong> <%= @listing.address %>
<strong>Last Updated:</strong> <%= @listing.updated_at %>
<br><br>
Thanks,<br>
Management
listing_needs_update.rake:
namespace :listings do
desc "Sends an email to the listing agent when that listing hasn't been updated in a month"
task listing_update_agent: :enviroment do
Listing.all.each do |listing|
if listing.updated_at == Date.today - 30
UpdateListingMailer.listing_update_agent(listing.listing_agent_id).deliver_now
end
end
end
end
错误:
追踪:
【问题讨论】:
-
你能显示一个错误,或者更详细地解释你的意思是“不工作”吗?
-
@maxple 我已经添加了错误。
-
你的任务不是命名为listing_update_agent,这意味着你应该将它作为rake listings:listing_update_agent 来调用??? Listing_needs_update 是从哪里来的?如果您可以在列表上创建一个范围会更加清晰和高效,例如 Listing.not_updated_in_last_month 这样您就不必询问每个列表记录是否超过一个月。您直接处理需要处理的记录。
-
@DarioBarrionuevo 哇...我觉得自己很愚蠢。这是文件名......这是漫长的一天!您能否详细说明创建范围的建议?
标签: ruby-on-rails ruby rake rake-task