I have a rails application what I want to send my users notifications when certain criteria are met. I can do this through a rake task. Currently I’m able to select the records that meet the criteria and email them to a specific address. The issue is that it sends ALL the records for all the accounts. Here’s what I have in the rake task:
task :send_reminds => :environment do
equipment = Equipment.where("calibration_date <= ?", Date.today)
EquipmentMailer.out_of_calibration(equipment).deliver
end
Here is the code for my 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
Here is the code for my HTML email (which works as expected):
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>
As you can see I’m sending the email directly to myself and receive the equipment list that meets the criteria. But that is unacceptable of course. I want the email to go to all the users within an account that has equipment that is out of calibration. Here are my models:
Equipment.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
User.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
I have tried something like this for the mail(:to => user.email) instead of a direct address while limiting the equipment list to be specific to an account and it’s users.
@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
The rake task will run without errors but I get no emails. Thoughts? BTW, I’m only about 1 month into rails so if I’m missing something extremely elementary you’ll have to forgive me.
My guess is that the problem lives in this code
You are not using the argument that is passed in to that method AT ALL.