Rails_learner Rails_learner - 1 month ago 21
Ruby Question

Mailer Challenge with Rails

I'm trying to send a mail to the owner of a store after each transaction. Everything else is set up, but i can't seem to get the email address generated.

class PaymentNotifier < ActionMailer::Base

def notify(payment_notification)
@payment_notification = payment_notification
mail to: **'xxxxxxxxx'**, subject: 'New order'
end
end


Payment Notification from Paypal comes into the system with details of the transaction:
cart_id, line_item_id, product_title.

Line_item has a cart_id column, as well as a product_id column.

Product has a store_id column.

Store has a user_id column.

User has an email address.

Trying to find out how i can access this email address from my mailer and fire the email after the transaction.

Any help would be appreciated.

Thanks in advance!

Answer

Referring your associations, you can get the user_email as,

LineItem.find(@payment_notification.line_item_id).product.store.user.email

Mailer,

class PaymentNotifier < ActionMailer::Base

 def notify(payment_notification)
    @payment_notification = payment_notification
     mail to: LineItem.find(@payment_notification.line_item_id).product.store.user.email, subject: 'New order'
  end
end