Rails/ActionMailer中的条件逻辑,用于向读者/订户提供同一文章的两个版本 [英] Conditional logic in Rails/ActionMailer for two versions of same article to readers/subscribers

查看:94
本文介绍了Rails/ActionMailer中的条件逻辑,用于向读者/订户提供同一文章的两个版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户是读者或订阅者.当发表新文章时,订户将收到全文电子邮件,读者将获得预告文本.假设该模型适用于读者/订户,这是对的,还是我需要针对每种类型的用户使用另一个条件?

Users are either readers or subscribers. When a new article is published, subscribers get a full text e-mail, readers get a teaser text. Assuming scopes in the model for readers/subscribers, is this right or do I need another conditional for each type of user?

if @article.valid?
  User.subscribers.each do |user|
    ArticleMailer.send_article_full(@article, user).deliver_now
  end
  User.readers.each do |user|
    ArticleMailer.send_article_teaser(@article, user).deliver_now
  end
  redirect_to :root, notice: "Article sent"
else
  render :new, notice: "There was an error"
end

推荐答案

基于事实,每个用户可以是阅读者或订阅者,但不能同时是两个用户,并且如果您为readers定义了正确的范围和User模型中的subscribers,那么应该没问题.对于每种类型的用户,您都不需要其他条件,因为那将是多余的.

Given the fact, every user can either be a reader or subscriber, but can't be both simultaneously, and if you have correct scopes defined for readers and subscribers in the User model, then it should be fine. You don't need another conditional for each type of user because that would be redundant.

这篇关于Rails/ActionMailer中的条件逻辑,用于向读者/订户提供同一文章的两个版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆