试图向特定用户发送邮件

时间:2014-12-20 00:28:50

标签: ruby-on-rails ruby actionmailer boolean-logic

如果这些布尔值都不成立,请尝试向所有用户发送邮件。如果它们中的任何一个都是真的,我们试图使邮件仅针对那些sports匹配真实布尔值的用户(例如:带有等于true的.football的文章将向那些用户发送电子邮件sport =“足球”)

def create
    @article = Article.new(article_params)
  if @article.save

    if (@article.football && @article.basketball && @article.volleyball && @article.lacrosse) == false
       all_users = User.all
       all_users.each do |user|
        ArticleMailer.new.async.article_confirmation(user,@article)
      end

    elsif @article.football == true 
        User.where( :sport => "Football").each do |user|
          ArticleMailer.new.async.article_confirmation(user,@article)
      end

    elsif @article.basketball == true    
      User.where("users.sport LIKE ?", "%Basketball%").each do |user|
        ArticleMailer.new.async.article_confirmation(user,@article)
      end

    elsif @article.volleyball == true 
      User.where( :sport => "Volleyball").each do |user|
        ArticleMailer.new.async.article_confirmation(user,@article)
      end

    elsif @article.lacrosse == true 
      User.where( :sport => "Lacrosse").each do |user|
        ArticleMailer.new.async.article_confirmation(user,@article)
      end


    end

    # send email to referral email
      # @allusers.each do |user|
      # ArticleMailer.new.async.article_confirmation(user,Article.find(5)) 
   redirect_to @article
    else
     render 'new'
    end
end

3 个答案:

答案 0 :(得分:1)

在这种情况下,我只会使用else块。因为这就是其他意思:以前的条件都不是真的

def create
  @article = Article.new(article_params)

  if @article.save
    users = if @article.football 
              User.where(:sport => "Football")
            elsif @article.basketball
              User.where("users.sport LIKE ?", "%Basketball%")
            elsif @article.volleyball
              User.where(:sport => "Volleyball")
            elsif @article.lacrosse
              User.where(:sport => "Lacrosse")
            else
              User.all
            end

    users.each do |user|
      ArticleMailer.new.async.article_confirmation(user, @article)
    end

    redirect_to @article

  else
    render :new
  end
end

此外,我建议将该逻辑移入模型中。这样可以更容易维护和测试:

# in models/artikel.rb
def notify_users
  users_interested_in_this_article.each do |user|
    ArticleMailer.new.async.article_confirmation(user, @article)
  end
end

private

def users_interested_in_this_article
  if football 
    User.where(:sport => "Football")
  elsif basketball
    User.where("users.sport LIKE ?", "%Basketball%")
  elsif volleyball
    User.where(:sport => "Volleyball")
  elsif lacrosse
    User.where(:sport => "Lacrosse")
  else
    User.all
  end
end

# in the controller
def create
  @article = Article.new(article_params)

  if @article.save
    @article.notify_users
    redirect_to @article
  else
    render :new
  end
end

答案 1 :(得分:0)

你正在检查错误。

我会改变

if (@article.football && @article.basketball && @article.volleyball && @article.lacrosse) == false

这样的事情

if @artical.football == false and @article.basketball == false and ... and ...

或者更好

if [@article.football,@article.basketball,@article.volleyball,@article.lacrosse].all? { |x| x == false }

希望这有帮助

答案 2 :(得分:0)

这里的代码

if (@article.football && @article.basketball && @article.volleyball && @article.lacrosse) == false

表示if ( Any one is false ) == false

您要做的是:

if ( !@article.football && !@article.basketball && !@article.volleyball && !@article.lacrosse )
   all_users = User.all
   all_users.each do |user|
    ArticleMailer.new.async.article_confirmation(user,@article)
  end
end