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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:55:24+00:00 2026-06-11T10:55:24+00:00

I have a very concrete dilemma right now. Given the following models: class Message

  • 0

I have a very concrete dilemma right now.

Given the following models:

class Message < ActiveRecord::Base
  attr_accessible :body, :sent_at

  belongs_to :subject
end

class Subject < ActiveRecord::Base
  attr_accessible :title

  has_many :messages
  belongs_to :last_message, :class_name => 'Message', :foreign_key => 'last_message_id'
end

In a view I want to iterate over a list of subjects and display:
– Subject title
– sent_at for the subject’s last message
like this:

<% @subjects.each do |subject| %>
  <%= subject.title %>
  <%= subject.last_message.sent_at %>
<% end %>

The thing is: subject.last_message may some times be nil. In which case, the above code will throw an exception.

So: What is the best solution to this? I can see 3 possibilities, but honestly don’t know which are considered good or bad.

1) Let the view rescue it

<%= subject.last_message.sent_at rescue '' %>

2) Make a helper

def last_message_sent_at(subject)
  return '' if subject.last_message.blank?
  subject.last_message.sent_at
end

<%= last_message_sent_at(subject) %>

3) Make a sort of “proxy” on the Subject model

class Subject < ...
  ...
  def last_message_sent_at
    return '' if last_message.blank?
    last_message.sent_at
  end
end

<%= subject.last_message_sent_at %>

Which would you choose, and why? Or is there perhaps another way, which I haven’t thought about?

/ Carsten

  • 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-06-11T10:55:25+00:00Added an answer on June 11, 2026 at 10:55 am

    Use try :

    <%= subject.last_message.try(:sent_at) %>
    

    So, if subject.last_message is nil, you will get no output; else if it is not nil, it will call the method sent_at on subject.last_message.

    It is like a convenient form for your #2 idea

    Documentation

    As additional thought, helper is a bad choice. You ideally always want a “receiver” (in some_class.perform(), some_class is the “receiver” i.e. it “receives” the message “perform”). I avoid Helpers unless I need to generate HTML. So, your #3 does have a receiver, but since Rails provides try, you do not need to roll your own.

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

Sidebar

Related Questions

I have very long integer sequences that look like this (arbitrary length!): 0000000001110002220033333 Now
I have a simple 'Buy Now' Google Checkout button on my Django site (very
My issue can be seen here: http://jasonnarciso.com/dev/cmwd/concrete-solutions/ Basically I have a very simple jquery
Have a very interesting situation and can't seem to find anything concrete on the
I have a matrix class very tailored for the algorithm I need to implement.
i have very serious problem if any tech expert can help...thank you in advance..
I have very simple piece of code. The goal is when i input four-digit
I have very very big html page/data. I need to fetch data under h1
I have very strange problem while I am submitting a practice problem on codechef.
I have very simple OpenGL ES example similar to Hehe's example : http://nehe.gamedev.net/tutorial/ios_lesson_02__first_triangle/50001/ As

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.