actionmailer - How to send email to all users who have an attribute as true (Rails Mailer) -


i'm trying send email newsletters users have left "subscribe newsletter" checked when registered. "subscribed" boolean attribute of "users" i'd use accomplish this.

so far have following.

the error:

undefined method `subscribed?' #<array:0x007f37ba707828>         mail to: user.pluck(:email).subscribed? 

in mailer:

class monthly_mailer < applicationmailer     def monthly(newsletter)         @newsletter = newsletter         mail to: user.pluck(:email).subscribed?     end end 

i've tried taking off '?' , have tried placing following method in both users model , controller

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

of course, changed self @user in controller , selected @user id.

please let me know if should post additional code. i'd know best way go goal, , not attached way of doing it.

thanks in advance input.

edit: added error

nomethoderror in newsletterscontroller#send_newsletter undefined method `ascii_only?' 1:fixnum

monthly_mailer.monthly(@newsletter).deliver_now

started "/send_newsletter?id=1" 127.0.0.1 @ 2015-07-07 11:30:20 -0400 started "/send_newsletter?id=1" 127.0.0.1 @ 2015-07-07 11:30:20 -0400 processing newsletterscontroller#send_newsletter html processing newsletterscontroller#send_newsletter html   parameters: {"id"=>"1"}   parameters: {"id"=>"1"}   newsletter load (0.2ms)  select  "newsletters".* "newsletters" "newsletters"."id" = $1 limit 1  [["id", 1]]   newsletter load (0.2ms)  select  "newsletters".* "newsletters" "newsletters"."id" = $1 limit 1  [["id", 1]]    (0.2ms)  select "users"."id" "users" "users"."subscribed" = 't'    (0.2ms)  select "users"."id" "users" "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  (0.5ms)  sent mail  (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?' 1:fixnum):   app/controllers/newsletters_controller.rb:8:in `send_newsletter'    nomethoderror (undefined method `ascii_only?' 1:fixnum):   app/controllers/newsletters_controller.rb:8:in `send_newsletter' 

as per http://guides.rubyonrails.org/action_mailer_basics.html (section 2.3.3 sending email multiple recipients)

the list of emails can array of email addresses or single string addresses separated commas.

1) define function in user model like:

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

2) in mailer, should doing this,

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

Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -