Rails 4 中的自定义 rake 任务问题
Issue with custom rake task in Rails 4
我正在尝试创建一个 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
错误:
跟踪:
我会在这里回答更清楚。
您的任务名为 listing_update_agent,这意味着您应按以下方式调用它:
rake listings:listing_update_agent
而不是:
rake listings:listing_needs_update
我还可以考虑对您的代码进行一些改进:
1) 任务做事情,所以将你的任务称为 notify_outdated_listings
更符合语义
2) 获取需要通知的列表并仅处理这些列表会更有效率,而不是像您在任务。
您可以通过在列表模型中创建范围来实现此目的,例如:
class Listing < ActiveRecord::Base
scope :not_updated_in_last_month, -> { where('updated_at >= ?', 1.month.ago) }
end
那么在你的任务中:
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.not_updated_in_last_month.each do |listing|
UpdateListingMailer.listing_update_agent(listing.listing_agent_id).deliver_now
end
end
end
您能说出此更改可能带来的性能差异吗?
我正在尝试创建一个 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
错误:
跟踪:
我会在这里回答更清楚。
您的任务名为 listing_update_agent,这意味着您应按以下方式调用它:
rake listings:listing_update_agent
而不是:
rake listings:listing_needs_update
我还可以考虑对您的代码进行一些改进:
1) 任务做事情,所以将你的任务称为 notify_outdated_listings
更符合语义2) 获取需要通知的列表并仅处理这些列表会更有效率,而不是像您在任务。 您可以通过在列表模型中创建范围来实现此目的,例如:
class Listing < ActiveRecord::Base
scope :not_updated_in_last_month, -> { where('updated_at >= ?', 1.month.ago) }
end
那么在你的任务中:
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.not_updated_in_last_month.each do |listing|
UpdateListingMailer.listing_update_agent(listing.listing_agent_id).deliver_now
end
end
end
您能说出此更改可能带来的性能差异吗?