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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:18:50+00:00 2026-05-25T15:18:50+00:00

I’m on Rails 3.0. For a model’s updated_at column, I need to output that

  • 0

I’m on Rails 3.0. For a model’s updated_at column, I need to output that as a user friendly format in JSON, so instead of seeing this 2011-09-14T22:01:32Z as the value, I would like to show less than a minute ago (or whatever user-friendly message I decide) for updated_at. I wrote a method that returns the output in the user-friendly format and now needs the functionality to be available in views, controller and model. What’s the best way to do this so it isn’t convoluted or bad practice.

EDIT Per answer from @klochner, here’s what my date.rb looks like but now it throws date.rb:21:informat’: wrong number of arguments (2 for 0) (ArgumentError)`

include ActionView::Helpers::DateHelper

class ActiveSupport::TimeWithZone
  def friendly
    format
  end
end

class Date
  def friendly
    format
  end
end

class Time
  def friendly
    format
  end
end

def format
  if self.today?
    format_time = ...
  elsif (self < Time.now.beginning_of_day) and (self > Time.now.yesterday.beginning_of_day)
    format_time = ...
  else
    format_time = ...
  end
end

@klochner suggestion changing format to format_helper works great. Thanks!

  • 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-25T15:18:51+00:00Added an answer on May 25, 2026 at 3:18 pm

    MonkeyPatch the ActiveSupport::TimeWithZone class, here’s what I do (I put this in config/initializers/date.rb)

    class ActiveSupport::TimeWithZone
      def mdy
        self.strftime("%m/%d/%Y")
      end
    end
    class Date
      def mdy
        self.strftime("%m/%d/%Y")
      end
    end
    class Time
      def mdy
        self.strftime("%m/%d/%Y")
      end
    end
    

    Then for any date object (or date field in an AR model), you can call mdy():

     > Time.now.mdy
     => "09/14/2011" 
    > User.first.created_at.mdy
     => "02/27/2009" 
    > Date.today.mdy
     => "09/14/2011" 
    

    For json, you probably want to write an as_json method for the model that you’re rendering. See this related answer:
    render :json does not accept options

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.