English 中文(简体)
习惯模式 方法、确定自动发送邮件的范围
原标题:Custom Model Method, setting scope for automatic sending of mail

这方面有几个阶段,而且由于我对铁路来说相对较新,如果我以最好的方式对待这一点,我是无法肯定的。

用户跟踪公司, 公司的申请在一定时间内开放和关闭。 如果一个用户跟一个公司接通,我就希望公司在申请截止日期之前一周(c))在公司申请结束之日自动收到电子邮件。

我尝试使用指定范围。 我有以下模式方法(我认为这不需要多少工作),根据日期确定每个公司的范围。

模范公司.rb

   def application_status
     if open_date == Today.date
      self.opening = true
     else
      self.opening = false
     end 

    if ((close_day - Today.date) == 7)
     self.warning = true
      else
     self.warning = false
    end 

    if close_day == Today.date
     self.closing = true
      else
     self.closing = false
    end 
 end

我希望每天向每个公司发出这一方法,以便每个公司都有适当的范围——因此,我尝试使用每当日(Cron)和以下代码。 在每个公司采用上述模式方法。

附表.rb

every 1.day do 
 runner "Firm.all.each do |firm|
  firm.application_status
 end"
end

然后,每次范围开放、警告、关闭时,在每份时间档案中都有一种方法,简而言之,我将只展示开张方法。 对所有拥有开放范围的企业,有以下询问,并采用公开-通知方法。

附表.rb

every 1.day do
 runner "Firm.opening.each do |firm|
  firm.application_open_notification
 end"
end

这在公司.rb模式中采用以下方法:

def application_open_notification
  self.users.each do |user|
   FirmMailer.application_open(user, self).deliver
  end
end

Which in turn calls the final piece of the puzzle... which should send the user an email, including the name of the firm.

def application_open(user,firm)
  @firm = firm
  @user = user
    mail to: @user.email, subject: @firm  is now accepting applications 
  end
 end

Is this a viable way to approach this problem? In particular I am not very familiar with coding in the model.

许多人感谢你能够提供的任何帮助。

最佳回答

www.un.org/Depts/DGACM/index_french.htm

class Firm < ActiveRecord::Base
    scope :opening, :where => { :opening => true }
    # etc
end

There is a general rule for database (and, well, all storage): don t store things you can caculate, if you don t have to.

由于申请地位可以从申请日期和<代码>开放_date/code>和close_day领域删除,你可以按需要计算这些申请,而不是为这些申请设立额外领域。 你们可以这样做,并积极记录:

scope :opening, :where { :open_date => (Date.today .. Date.today+1) }
scope :warning, :where { :close_day => (Date.today+7 .. Date.today+8) }
scope :closing, :where { :close_day => (Date.today .. Date.today+1) }

(说明这些选择的时间范围。) 如果您使用<条码>,<>/代码>或<条码> 时领域,则须加以改动。

但还有一个问题:如果由于某种原因(计算机坠毁、编码 b等),你预定的方案在某一天没有运行,情况如何? 你们需要一种方式,确保最终发出通知,即使有点中断。 有两个解决办法:

  1. Write your schedule program to optionally accept a date besides today (via ARGV)
  2. keep flags for each firm for whether each kind of notice has been sent. These will have to be stored in the databse.

说明范围是必要的。 你能够这样做:

Firm.where(:open_date => (Date.today .. Date.today+1)).each do |firm|
   #...
end

但范围至少概括了确定各类记录的细节。

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签