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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:10:10+00:00 2026-06-03T01:10:10+00:00

I had to implement these methods on an ActiveRecord model in order to support

  • 0

I had to implement these methods on an ActiveRecord model in order to support some desired view functionality.

I’m thinking there has to be a more compact way to declare these methods, probably involving an Array of symbols and an Array#each call.

Any suggestions?

(These are legacy column names, BTW.)


class Contract < ActiveRecord::Base

  def start_date_displayed
    self.start_date.present? ? self.start_date.strftime("%-m/%-d/%Y") : ''
  end

  def start_date_displayed=(input)
    if input.present?
      self.start_date = Date.strptime input, '%m/%d/%Y'
    end
  end

  def end_date_displayed
    self.end_date.present? ? self.end_date.strftime("%-m/%-d/%Y") : ''
  end

  def end_date_displayed=(input)
    if input.present?
      self.end_date = Date.strptime input, '%m/%d/%Y'
    end
  end

  def ArrivalDate_displayed
    self.ArrivalDate.present? ? self.ArrivalDate.strftime("%-m/%-d/%Y") : ''
  end

  def ArrivalDate_displayed=(input)
    if input.present?
      self.ArrivalDate = Date.strptime input, '%m/%d/%Y'
    end
  end

  def Contract_ReceivedDate_displayed
    self.Contract_ReceivedDate.present? ? self.Contract_ReceivedDate.strftime("%-m/%-d/%Y") : ''
  end

  def Contract_ReceivedDate_displayed=(input)
    if input.present?
      self.Contract_ReceivedDate = Date.strptime input, '%m/%d/%Y'
    end
  end

  def Contract_FinalizedDate_displayed
    self.Contract_FinalizedDate.present? ? self.Contract_FinalizedDate.strftime("%-m/%-d/%Y") : ''
  end

  def Contract_FinalizedDate_displayed=(input)
    if input.present?
      self.Contract_FinalizedDate = Date.strptime input, '%m/%d/%Y'
    end
  end

  def Committee_Date_displayed
    self.Committee_Date.present? ? self.Committee_Date.strftime("%-m/%-d/%Y") : ''
  end

  def Committee_Date_displayed=(input)
    if input.present?
      self.Committee_Date = Date.strptime input, '%m/%d/%Y'
    end
  end

  # ...

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-06-03T01:10:11+00:00Added an answer on June 3, 2026 at 1:10 am
    class Contract < ActiveRecord::Base
    
      %w(start_date end_date).each do |attr_name|
         class_eval <<-METHOD_DEF
             def #{attr_name}_displayed
                 self.#{attr_name}.present? ? self.#{attr_name}.strftime("%-m/%-d/%Y") : ''
             end
    
             def #{attr_name)_displayed=(input)
                if input.present?
                  self.#{attr_name} = Date.strptime #{attr_name}, '%m/%d/%Y'
                end
             end
    
    
         METHOD_DEF
      end
    end
    

    If only define these methods for those columns that name ends with date, then

    class Contract < ActiveRecord::Base
    
       self.column_names.grep(/date$/i).each do |column_name|
    
         class_eval <<-METHOD_DEF
             def #{column_name}_displayed
                 self.#{column_name}.present? ? self.#{column_name}.strftime("%-m/%-d/%Y") : ''
             end
    
             def #{column_name)_displayed=(input)
                if input.present?
                  self.#{column_name} = Date.strptime #{column_name}, '%m/%d/%Y'
                end
             end             
         METHOD_DEF
    
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had try to implement the send me log feature into my apps but
I had asked a question on how to implement real time updates in ASP.NET
I am trying to implement a image recognition program in C++ . I had
Need some help with this problem in implementing with XSLT, I had already implemented
I had started working on GPGPU some days ago and successfully implemented cholesky factorization
I have some entities created with LINQ-to-SQL. Six of these entities (representing values primarily
Hay i need to hand implemeneting a voting system into a model. I've had
I have tried many methods to implement a regular UISlider and control the device
These types of methods have always bothered me. Without digging through the code, I
UPDATE: With the blush of shame I discovered that the order had nothing to

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.