如何向所有属性为true的用户发送电子邮件(Rails Mailer)

时间:2015-07-07 14:32:47

标签: ruby-on-rails actionmailer newsletter

我试图向所有离开"订阅简报的用户发送电子邮件简报"他们注册时检查。 "已订阅和#34;是"用户"的布尔属性。我想用它来实现这个目标。

到目前为止,我有以下内容。

错误:

undefined method `subscribed?' for #<Array:0x007f37ba707828>
        mail to: User.pluck(:email).subscribed?

在我的邮件中:

class Monthly_Mailer < ApplicationMailer
    def monthly(newsletter)
        @newsletter = newsletter
        mail to: User.pluck(:email).subscribed?
    end
end

我试过脱掉&#39;?&#39;并尝试将以下方法放入我的users modelcontroller

def subscribed?
    true if self.subscribed == true
end

当然,我在控制器中将self更改为@user,并按@user选择了id

如果我要发布其他代码,请告诉我。我只想知道实现这一目标的最佳方式,并不一定非常依赖这种做法。

提前感谢您的意见。

编辑:添加了错误

  

NewslettersController中的NoMethodError#send_newsletter   未定义的方法`ascii_only?&#39; 1:Fixnum

     

Monthly_Mailer.monthly(@newsletter).deliver_now

Started GET "/send_newsletter?id=1" for 127.0.0.1 at 2015-07-07 11:30:20 -0400
Started GET "/send_newsletter?id=1" for 127.0.0.1 at 2015-07-07 11:30:20 -0400
Processing by NewslettersController#send_newsletter as HTML
Processing by NewslettersController#send_newsletter as HTML
  Parameters: {"id"=>"1"}
  Parameters: {"id"=>"1"}
  Newsletter Load (0.2ms)  SELECT  "newsletters".* FROM "newsletters" WHERE "newsletters"."id" = $1 LIMIT 1  [["id", 1]]
  Newsletter Load (0.2ms)  SELECT  "newsletters".* FROM "newsletters" WHERE "newsletters"."id" = $1 LIMIT 1  [["id", 1]]
   (0.2ms)  SELECT "users"."id" FROM "users" WHERE "users"."subscribed" = 't'
   (0.2ms)  SELECT "users"."id" FROM "users" WHERE "users"."subscribed" = 't'
  Rendered monthly_mailer/monthly.html.erb within layouts/mailer (0.3ms)
  Rendered monthly_mailer/monthly.html.erb within layouts/mailer (0.3ms)

Monthly_Mailer#monthly: processed outbound mail in 5.4ms

Monthly_Mailer#monthly: processed outbound mail in 5.4ms

Sent mail to  (0.5ms)

Sent mail to  (0.5ms)


Completed 500 Internal Server Error in 8ms (ActiveRecord: 0.4ms)
Completed 500 Internal Server Error in 8ms (ActiveRecord: 0.4ms)

NoMethodError (undefined method `ascii_only?' for 1:Fixnum):
  app/controllers/newsletters_controller.rb:8:in `send_newsletter'



NoMethodError (undefined method `ascii_only?' for 1:Fixnum):
  app/controllers/newsletters_controller.rb:8:in `send_newsletter'

4 个答案:

答案 0 :(得分:2)

如果您想在to字段中向所有订阅者发送1封电子邮件,请使用此选项。

mail(to: User.where(subscribed: true).pluck(:email))

如果您希望每个订阅者单独发送电子邮件

,请使用此选项
User.where(subscribed: true).pluck(:email).each do |email|
  mail(to: email)
end

您收到ascii_only?错误的原因是因为@ user3683877的回答是传递了一组ID而不是电子邮件地址。

答案 1 :(得分:1)

好的,如果你想使用pluck:  mail to: User.where(activated: true).pluck(:email)

要获得更好的代码,请使用范围而不是where(activated: true)

#user.rb scope :activated, -> { where(activated: true) }

然后  mail to: User.activated.pluck(:email)

答案 2 :(得分:1)

首先你错了:

1)在您的邮件程序操作中,您正在收集用户的电子邮件,这将最终返回一系列电子邮件,然后您链接了您在User类中定义的订阅?功能。因此,它必然会出错。

User.pluck(:email).subscribed?

解决方案:

1)在用户模型中定义一个函数,如:

def self.subscribed_users
  User.where(subscribed: true).pluck(:email)
end

2)在您的邮件中,您可以这样打电话:

def monthly(newsletter)
        @newsletter = newsletter
        mail to: User.subscribed_users
end

答案 3 :(得分:1)

根据http://guides.rubyonrails.org/action_mailer_basics.html(第2.3.3节向多个收件人发送电子邮件)

电子邮件列表可以是一组电子邮件地址,也可以是一个字符串,地址以逗号分隔。

1)在用户模型中定义一个函数,如:

def self.subscribed_users
  User.where(subscribed: true)
end

2)在你的邮件中,你应该这样做,

def monthly(newsletter)
    @newsletter = newsletter
    mail to: User.subscribed_users.pluck(:email)
    # uncomment the below line and comment above line to get comma separated emails
    # mail to: User.subscribed_users.collect(&:email).join(',')
end