Reputation: 545
I'm trying to send emails to users who subscribe to other users when they post.
I'm getting an error:
ActiveRecord::RecordNotFound - Couldn't find User with 'id'=272:
272 is the id of one of the subscriptions. I'm getting confused with all the moving parts here. How do I pull the subscribers email?
Posts Controller
def create
@post = current_user.posts.new(post_params)
if @post.save
@subscribers = current_user.subscribers
@subscribers.each do |f|
SubscriptionMailer.send_prayer_request(@post, f).deliver
end
[...]
Subscription Mailer
class SubscriptionMailer < ActionMailer::Base
def send_prayer_request(post, subscriber)
@u = User.find(subscriber)
mail( :to => @u.email,
:from => "[email protected]",
:subject => "New Post! "
)
end
end
User Model
def subscribers
Subscription.where(subscribe_to_id: self.id)
end
Schema for relevant tables
create_table "subscriptions", force: :cascade do |t|
t.integer "user_id"
t.integer "subscribe_to_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "username", default: "", null: false
t.string "first_name"
t.string "last_name"
end
Thanks for any help you can offer.
Upvotes: 0
Views: 56
Reputation: 2629
Change @u = User.find(subscriber)
to @u = User.find(subscriber.user_id)
Otherwise, you are trying to find a user with an ID which matches the subscriber's ID (different from the subscriber's user_id).
Upvotes: 1