特定の基準が満たされたときにユーザーに通知を送信したい Rails アプリケーションがあります。これは、rake タスクを介して行うことができます。現在、基準を満たすレコードを選択して、特定のアドレスに電子メールで送信できます。問題は、すべてのアカウントのすべてのレコードを送信することです。これが私がレーキタスクに持っているものです:
task :send_reminds => :environment do
equipment = Equipment.where("calibration_date <= ?", Date.today)
EquipmentMailer.out_of_calibration(equipment).deliver
end
私のEquipmentMailerのコードは次のとおりです。
class EquipmentMailer < ActionMailer::Base
default :from => "mark.odonnell@azzurgroup.com"
def out_of_calibration(equipment)
@equipment = Equipment.where("calibration_date <= ?", Date.today)
mail(:to => "markaodonnell@gmail.com", :subject => "Equipment is out of calibration")
end
end
これが私のHTMLメールのコードです(期待どおりに機能します):
The following Equipment is Due:
<br></br>
<table>
<tr>
<th>Equipment Name</th>
<th> </th>
<th>Calibration Due Date</th>
</tr>
<% @equipment.each do |equipment| %>
<tr>
<td><%= equipment.equipment_id %></td>
<td> </td>
<td><%= equipment.calibration_date %></td>
</tr>
<% end %>
</table>
ご覧のとおり、私は自分自身に直接メールを送信し、基準を満たす機器リストを受け取ります。しかし、それはもちろん受け入れられません。アカウント内の機器が調整されていないすべてのユーザーにメールを送信したいです。ここに私のモデルがあります:
機器.rb
class Equipment < ActiveRecord::Base
acts_as_tenant(:account)
validates :equipment_id, presence: true
validates :location, presence: true
validates_uniqueness_to_tenant :serial_number
has_many :assets, :dependent => :destroy
accepts_nested_attributes_for :assets, :allow_destroy => true
has_paper_trail
def self.text_search(query)
if query.present?
search(query)
else
scoped
end
end
ユーザー.rb
class User < ActiveRecord::Base
acts_as_tenant(:account)
validates_uniqueness_to_tenant :email
attr_accessible :name, :email, :password, :password_confirmation, :title, :company,
:phone, :mobile, :admin
has_secure_password
before_save :create_remember_token
belongs_to :account
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
# has_paper_trail
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
Account.rb
class Account < ActiveRecord::Base
attr_accessible :subdomain, :email
VALID_SUBDOMAIN_REGEX = /\A[\w+\-.]+(-[a-z\d])+(-[a-z\d])/i
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :subdomain, :presence => true,
:uniqueness => true
validates :email, :presence => true,
format: { with: VALID_EMAIL_REGEX }
validates_presence_of :plan_id
belongs_to :plan
has_many :users
has_many :equipment, :through => :users
before_save { |account| account.subdomain = account.subdomain.downcase }
end
アカウントとそのユーザーに固有の機器リストを制限しながら、直接アドレスの代わりにメール(:to => user.email)に対してこのようなことを試しました。
@equipment = Equipment.where("calibration_date <= ?", Date.today)
@equipment.each do |equipment|
equipment.accounts.each do |account|
accounts.users.each do |user|
user.email.each do |email|
mail(:to => email, :subject => "Equipment is out of calibration"
end
end
end
end
rake タスクはエラーなしで実行されますが、メールが届きません。考え?ところで、私はレールに約 1 か月しか乗っていないので、非常に初歩的なことを見逃している場合は、ご容赦ください。