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

  • SEARCH
  • Home
  • 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 782843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:26:59+00:00 2026-05-14T20:26:59+00:00

Quite possibly there could be a rails magic I’ve missed, but I’m guessing it

  • 0

Quite possibly there could be a rails magic I’ve missed, but I’m guessing it will be in Ruby.

I have a model called Company which has_many Contacts.

Suppose Company has Contact 1, Contact 2, Contact 3, and Contact 4.

When I create a textblog for each Contact, I want to output the following (where Contact = Contact 1)

“Hi, Contact 1,
I am also writing to Contact 2, Contact 3, and Contact 4.”

So it needs to extract the Contact in the salutation and then list them, inserting “and” before the last Contact in the list.

Edit

I now have the following:

This is in contacts_controller.rb

  def full_name
   [self.first_name, self.last_name].compact.join(" ")
  end

in contact_letters_controllers.rb (contact_letters are the executed contacts with letters)

  def new

    @contact_letter = ContactLetter.new
    @contact_letter.contact_id = params[:contact]
    @contact_letter.letter_id = params[:letter]

    @contact = Contact.find(params[:contact])
    @company = Company.find(@contact.company_id)

    contacts = @company.contacts.collect(&:full_name)

    contacts.each do |contact|
       @colleagues = contacts.reject{ |c| c==@contact.full_name }
    end

    @letter = Letter.find(@contact_letter.letter_id)

    @letter.body.gsub!("{FirstName}", @contact.first_name)
    @letter.body.gsub!("{Company}", @contact.company_name) 
    @letter.body.gsub!("{Colleagues}", @colleagues.to_sentence)

    @contact_letter.body = @letter.body
    @contact_letter.status = "sent"

  end 
  • 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-14T20:27:00+00:00Added an answer on May 14, 2026 at 8:27 pm

    I believe to_sentence is what you’re looking for.

    You could try something like the following:

    contacts = Company.contacts.collect(&:name)
    
    contacts.each do |contact|
       other_contacts = contacts.reject{ |c| c==contact }
       p "Hi, #{contact}, I am also writing to #{other_contacts.to_sentence}"
    end
    

    I didn’t test this code, so forgive me if this doesn’t work.

    Edit

    This method will return the contact’s full_name. If you also have a middle_name attribute, you can add on the array. This method will still work if any of the attributes is nil.

    def full_name
       [self.first_name, self.last_name].compact.join(" ")
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could quite possibly be the dumbest question ever asked. Our client wants us
This is quite possibly a very stupid question but I've tried searching for and
It is quite possible that I may not have got the point, but I
Nikhil Kothari's Script# is quite possibly one of the most amazing concepts I've seen
It's quite possible a question like this has been asked before, but I can't
is it possibly in c# to have some sort of base class functionality which
I wonder if you could help me. I have a list of data that
In my application I have quite some void-pointers (this is because of historical reasons,
I'm looking for recommendations for vendors - quite possibly start-ups - who can help
I'm writing my first rails plugin and could use a little help. In a

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.