Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7398419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:52:03+00:00 2026-05-29T03:52:03+00:00

I spent hours on this and I just cannot get it: What I am

  • 0

I spent hours on this and I just cannot get it:

What I am trying to do is; email all users who have made a comment on a discussion board whenever a new comment is posted.

I am using user_mailer.rb

def new_post_from_buyer(post)
  @post = post
  users = User.all.posts.where(:project_id => post.project.id)
  mail(:to        => 'support@freelancify.com',
       :bcc       => #Somehow turn the users variable i just named into an array of their emails
       :subject   => 'The Project Poster Posted A New Comment')
end

I have the UserMailer.new_post_from_buyer(@post).deliver correctly placed in the posts_controller.rb

So there’s a few things that has to happen which I cannot for the life of me get to work successfully.

1 – I have to get all the Users who’s Posts match the current Project. (to clarify, a Project contains a discussion board in which where all the posts go) The current code in the mailer throws a undefined method for ‘posts’, and every other way I tried won’t work.

2 – I then have to take those Users and extract their emails (which is a column in the Users table)

3 – I need to then be able to take all their emails and turn it into an array separated by commas so I can throw it in the :bcc of the user_mailer.rb.

How would you go about getting this to work? A new way with .map or some method I have no idea about, fixing up the code I think I need?

I am running Rails 3.1.

To further clarify:

  • User has_many Posts.
  • Project has_many Posts.
  • Post belongs_to User.
  • Post belongs_to Project.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T03:52:03+00:00Added an answer on May 29, 2026 at 3:52 am

    This may not be the most efficient way to do it, as it is generally better to use database commands to comb data, but based on your models and relationships you could use ruby methods to do something like this:

    def new_post_from_buyer(post)
      # This assumes that the attribute on the user that holds the email is called 'email'
      array_of_user_emails = post.project.posts.map { |pos| pos.user.email }
      mail(:to        => 'support@freelancify.com',
           :bcc       => array_of_user_emails,
           :subject   => 'The Project Poster Posted A New Comment')
    end
    

    I think you were also on the right track in your attempt, just with wrong syntax. This might actually be a little more efficient:

    def new_post_from_buyer(post)
      # This assumes that the attribute on the user that holds the email is called 'email'
      array_of_user_emails = User.includes(:posts).select(:email).where('posts.project_id = ?', post.project_id).map(&:email)
      mail(:to        => 'support@freelancify.com',
           :bcc       => array_of_user_emails,
           :subject   => 'The Project Poster Posted A New Comment')
    end
    

    This would probably be even more efficient than my first example since it uses SQL to do the searching for the email. This would only work if you were using ActiveRecord with a SQL based database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent hours trying to figure this problem and i just can't, so
Ok so I've spent a couple hours trying to resolve this issue and have
Is there another way to do this? Just spent 2 hours trying to figure
spent a few hours trying to figure this out, but cannot for the life
So I am pretty new to JQuery and just spent 5 hours getting this
I hope this is a relatively easy problem although I have spent hours websearching
this may sound funny, but i spent hours trying to recreate a knob with
I spent about 4 hours yesterday trying to fix this issue in my code.
So yeah, like I said, I've spent a few hours trying to fix this
I have spent too many hours on this and I have so far continued

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.