Ruby on rails RubyonRails在添加带有用户首选项的新记录时向用户发送电子邮件

Ruby on rails RubyonRails在添加带有用户首选项的新记录时向用户发送电子邮件,ruby-on-rails,ruby,ruby-on-rails-3,email,parameters,Ruby On Rails,Ruby,Ruby On Rails 3,Email,Parameters,在我的RubyonRails应用程序中,我正在创建一个电影院网站。用户可以在“首选项”表中保存他们最喜欢的演员,我尝试的是获取该演员,这样,如果添加了一部包含该演员的新电影,则将该演员作为他们最喜欢的演员的所有用户都会通过电子邮件收到该电影的详细信息 我已经有了一个电子邮件系统,用户可以通过电子邮件确认他们已成功注册 我遇到的问题是,我需要通过控制器检索添加到新电影记录中的演员,如果这符合用户的偏好,则发送电子邮件给他们。但在保存之前,我正在努力检查电影的参数 到目前为止,我已在applicat

在我的RubyonRails应用程序中,我正在创建一个电影院网站。用户可以在“首选项”表中保存他们最喜欢的演员,我尝试的是获取该演员,这样,如果添加了一部包含该演员的新电影,则将该演员作为他们最喜欢的演员的所有用户都会通过电子邮件收到该电影的详细信息

我已经有了一个电子邮件系统,用户可以通过电子邮件确认他们已成功注册

我遇到的问题是,我需要通过控制器检索添加到新电影记录中的演员,如果这符合用户的偏好,则发送电子邮件给他们。但在保存之前,我正在努力检查电影的参数

到目前为止,我已在application controller中执行了以下操作,以查找用户的电子邮件,其中演员Benedict Cumberbatch是他们的最爱:

def actor_user
    actor = "Benedict Cumberbatch"
    user = Perference.where(actor: actor).pluck(:user_id)
    User.where(id: user).pluck(:email)
end
这也有一个问题,演员是硬编码的,而我想做的是从电影演员中提取出电影中三个演员的名字,并通过这些名字进行检查

自动控制器:

def create
    @film = Film.new(film_params)
    if @film.save
        redirect_to @film
    else
        render 'new'
    end
end
用户_Mailer.rb

class UserMailer < ApplicationMailer
  default from: 'no-reply@thorcinemas.com'

  def welcome_email(user)
    @user = user
    @url  = 'http://localhost:3000/users/login'
    # mail(to: @user.email, subject: 'Welcome to My Awesome Site')
    mail(to: @user.email, subject: 'Welcome to My Awesome Site')
  end
end
有人能帮忙吗。我知道我需要一封新的电子邮件(因为目前我只有welcome_email.html.erb),所以需要在适当的时候发送这些邮件

这是模式:

  create_table "films", force: :cascade do |t|
     t.string   "title"
     t.string   "synopsis"
     t.string   "director"
     t.string   "cast1"
     t.string   "cast2"
     t.string   "cast3"
     t.date     "release_date"
     t.string   "warnings"
     t.datetime "created_at",     null: false
     t.datetime "updated_at",     null: false
     t.string   "image_url"
     t.string   "certificate_id"
     t.integer  "category_id"
     t.integer  "hours"
     t.integer  "minutes"
     t.string   "video_url"
   end

   create_table "perferences", force: :cascade do |t|
     t.integer  "user_id"
     t.integer  "category_id"
     t.datetime "created_at",  null: false
     t.datetime "updated_at",  null: false
     t.text     "colour"
     t.text     "actor"
   end

 create_table "users", force: :cascade do |t|
    t.string   "password_digest"
    t.string   "role"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "first_name"
    t.string   "last_name"
    t.string   "house_no"
    t.string   "street"
    t.string   "town"
    t.string   "postcode"
    t.string   "email"
    t.date     "date_of_birth"
  end

我知道我打破了最佳做法,在电影表中加入了actor1、actor2、actor3,而不是演员表,但出于这个问题的目的,请忽略此问题。

因此,如果我理解正确,在添加了一些演员的电影后,应该通过电子邮件通知一些用户

我想我能帮你。创建后,您可以在
中编写逻辑

class FilmObserver < ActiveRecord::Observer
  def after_create(film)
    # 1. fetch actors from film
    actors = [film.cast1, film.cast2, film.cast3]

    # 2. find users that should be notified
    user_ids = Perference.where(actor: actor).pluck(:user_id)
    user_emails = User.where(id: user).pluck(:email)

    # 3. send them emails
    UserMailer.send_notifications(user_emails)
  end
end
class FilmObserver
在用户_mailer.rb中

class UserMailer < ApplicationMailer
  # other things

  def send_notifications(emails)
    mail(to: emails, subject: 'My Shiny Notification')
  end
end
class UserMailer

当然,这段代码可以改进,我只是想表达我的想法。=)

是的,这正是我想要做的。我已经读过并遵循了它,但对于如何实现它却没有任何线索it@benjs.1我在帖子中添加了一些代码,也许现在我的想法会更清晰=)
class FilmObserver < ActiveRecord::Observer
  def after_create(film)
    # 1. fetch actors from film
    actors = [film.cast1, film.cast2, film.cast3]

    # 2. find users that should be notified
    user_ids = Perference.where(actor: actor).pluck(:user_id)
    user_emails = User.where(id: user).pluck(:email)

    # 3. send them emails
    UserMailer.send_notifications(user_emails)
  end
end
class UserMailer < ApplicationMailer
  # other things

  def send_notifications(emails)
    mail(to: emails, subject: 'My Shiny Notification')
  end
end