Conditional logic in Rails/ActionMailer for two versions of same article to readers/subscribers

戏子无情 提交于 2019-12-13 07:00:11

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/32168657/conditional-logic-in-rails-actionmailer-for-two-versions-of-same-article-to-read

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!