【发布时间】:2017-09-28 21:06:04
【问题描述】:
我无法仅向布尔值设置为 true 的某些用户发送电子邮件群发邮件,而无法将电子邮件发送给布尔值设置为 false 的用户。
在我的应用中,我有粉丝通过艺术家关系关注艺术家。在我的 ArtistRelationship 模型中,我有一个布尔值,粉丝可以根据他们是否希望在艺术家发帖时发送来自艺术家的电子邮件来将其设置为真或假。
到目前为止,我有这个:
艺术家.rb
class Artist < ApplicationRecord
def self.fan_post_email
Artist.inlcudes(:fans).find_each do |fan|
fan.includes(:artist_relationships).where(:post_email => true).find_each do |fan|
FanMailer.post_email(fan).deliver_now
end
end
end
end
posts_controller.rb
class Artists::PostsController < ApplicationController
def create
@artist = current_artist
@post = @artist.artist_posts.build(post_params)
if @post.save
redirect_to artist_path(@artist)
@artist.fan_post_email
else
render 'new'
flash.now[:alert] = "You've failed!"
end
end
end
我无法让fan_post_email 方法工作。我不完全确定如何在 ArtistRelationship 模型中找到布尔值设置为 true 的粉丝。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-5