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 798685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:00:40+00:00 2026-05-14T23:00:40+00:00

everyone: I am also open to just straight-up refactoring what I’m finding to be

  • 0

everyone: I am also open to just straight-up refactoring what I’m finding to be pretty repetitive, but to give a baseline of how it’s working….

I have for every contact a Campaign, which has_many of three types of Models: Email, Call, and Letter.

When an Email (Call or Letter) has been executed for a specific contact, I have a Contact_Email(_or_Call_or_Letter) which belongs to both the Contact and the Model (Email_or_Call_or_Letter).

Each Contact_Email for example pairing has a :date_sent attribute. So does each Contact_Call and Contact_Letter.

How do I find the latest of all of them?

Here is the code I wrote that can find the latest Email and my finding retyping similar code for Call and Letter, but then stuck on how to do a .max on all of them:

  def last_email(contact)
    #get campaign the contact belongs to
    @campaign = Campaign.find_by_id(contact.campaign_id)

    @last_email = ContactEmail.find(:last, 
                        :conditions => "contact_id = #{contact.id}",
                        :order => "date_sent DESC")

    @last_call = ContactCall.find(:last, 
                        :conditions => "contact_id = #{contact.id}",
                        :order => "date_sent DESC")

    @last_letter = ContactLetter.find(:last, 
                        :conditions => "contact_id = #{contact.id}",
                        :order => "date_sent DESC")

    # how do I get the latest of all of these to display?

    @email_template = Email.find_by_id(@last_email.email_id)

    if @last_email.nil?
      return "no email sent"
    else
      return @last_email.date_sent.to_s(:long) + link_to('email was sent', @email_template)
    end
  end

Question 1: With what I have, how can I find effectively @last_event given I can find the last Email, last Call, and last Letter for every contact?

Question 2: How can I remove the repetitive code that I have to write for each Model?

  • 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-14T23:00:40+00:00Added an answer on May 14, 2026 at 11:00 pm

    Do you have has_many associations setup in Contact referring to the other models? Something like:

    class Contact < ActiveRecord::Base
      has_many :contact_emails
      has_many :contact_calls
      has_many :contact_letters
    end
    

    If so, you can then create a last_event method on the Contact model:

    def latest_event
      [contact_emails, contact_calls, contact_letters].map do |assoc|
        assoc.first(:order => 'date_sent DESC')
      end.compact.sort_by { |e| e.date_sent }.last
    end
    

    Handling nil

    When using the latest_event method you will get nil if there are no associated records. There are a couple of ways you can workaround this. The first is to check for nil first with something like:

    contact.latest_event && contact.latest_event.date_sent
    

    On late versions of Rails/Ruby you can also use Object#try which will call the method if it exists:

    contact.latest_event.try(:date_sent)
    

    I prefer not to use this as it doesn’t check for nil but only if the object can respond to a method. This has cause some interesting errors if you expect nil if the object is nil but are calling a method which nil itself responds to.

    Finally, my preferred method for the simple case is to use the andand gem which provides Object#andand. This greatly shortens the safe case above and saves calling of latest_event multiple times:

    contact.latest_event.andand.date_sent
    

    date_sent, nil and You.

    For your example usage of calling to_s(:long), you could either use && or andand:

    contact.latest_event.andand.date_sent.andand.to_s(:long)
    

    or

    contact.latest_event && contact.latest_event.date_sent.to_s(:long)
    

    The first is safer if date_sent itself may be nil. Without using andand this could be written as:

    contact.latest_event &&
      contact.latest_event.date_sent &&
      contact.latest_event.date_sent.to_s(:long)
    

    which is rather complex and unwieldily in my opinion. I would recommend looking into andand

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

Sidebar

Related Questions

everyone! I have sprite moving by action, what have health bar (progress bar). When
everyone seems interested in building IPhone apps today. Do you have to have an
everyone. I'm just starting with django, so sorry for a possibly stupid question. Imagine,
hi everyone i have a problem with my streaming. at the moment i have
I have a project that references a number of open source libraries, some new,
Hello everyone and thanks up front for your time, I am working on a
what kind of questions do I need to ask. Also, I don't want everyone
Everyone is saying how .NET Remoting is being replaced by WCF, but I'm wondering
This is basic programming question. I'm a PHP developer, but this is open for
Lets say you have a site where users may open chat rooms. Once 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.