views:

27

answers:

2

I have a problem, I'll simplify it down to the blog example.
1) Each 'post' is created by a different user stored in a 'User' database.
2) Several 'comments' belong to the one 'post'.
3) When a new 'comment' is made, the user who created the initial 'post' needs to be emailed.

I have setup the mailer and observer. I am just not sure how to get the user of the post's email address once a new post is made.

Any help would be wonderful (and I hope this makes sense).

+3  A: 

I assume you have associations set up like this

class User
  has_many :posts
end

class Post
  belongs_to :user
  has_many :comments
end

class Comment
  belongs_to :post
end

Then in your mailer just find the user's e-mail address through these associations

class CommentMailer < ActionMailer::Base
  def comment_notification(comment)
    recipients comment.post.user.email
    # Other mail sending methods
  end
end

And once new comment is being created

comment = Comment.create(attributes)
CommentMailer.deliver_comment_notification(comment)
Priit
+1 Great answer!
Doug Neiner
A: 

Thanks for the great answer, Ruby on Rails seems to logical sometimes :P

vectran