在Rails应用程序中满足条件时,向帐户中的多个用户发送电子邮件

时间:2012-05-02 16:29:20

标签: ruby-on-rails ruby ruby-on-rails-3 actionmailer ruby-on-rails-3.2

我有一个rails应用程序,当满足某些条件时,我想向用户发送通知。我可以通过rake任务完成这项工作。目前,我可以选择符合条件的记录,并通过电子邮件将其发送到特定地址。问题是它发送所有帐户的所有记录。这是我在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>

如您所见,我将电子邮件直接发送给自己并收到符合条件的设备列表。但这当然是不可接受的。我希望将电子邮件发送给帐户中的所有用户,这些用户的设备已经过校准。这是我的模特:

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

我已经为邮件(:to =&gt; 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任务将运行没有错误,但我没有收到任何电子邮件。思考?顺便说一句,我只有大约一个月的时间进入轨道,所以如果我错过了一些非常基本的东西,你将不得不原谅我。

2 个答案:

答案 0 :(得分:0)

我的猜测是问题存在于此代码中

def out_of_calibration(equipment)
    @equipment = Equipment.where("calibration_date <= ?", Date.today)
    mail(:to => "markaodonnell@gmail.com", :subject => "Equipment is out of calibration")
  end

您没有使用传递给该方法的所有参数。

答案 1 :(得分:0)

您的关联看起来不正确,看起来帐户应该有多个设备和帐户应该有多个用户。用户应该属于帐户,设备应该属于帐户。设备可以属于多个账户吗?没有意义。我建议如下:

class Account < ActiveRecord::Base
 has_many :users
 has_many :equipments
end

class User < ActiveRecord::Base
  belongs_to :account
end

class Equipment < ActiveRecord::Base
  belongs_to :account
  has_many :equipments, :through => :account
end

@equipment = Equipment.where("calibration_date <= ?", Date.today)  
  @equipment.each do |equipment|
    equipment.users.each do |user|
      mail(:to => user.email, :subject => "Equipment is out of calibration"
  end
end