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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:52:01+00:00 2026-05-28T04:52:01+00:00

I have a model with several date attributes. I’d like to be able to

  • 0

I have a model with several date attributes. I’d like to be able to set and get the values as strings. I over-rode one of the methods (bill_date) like so:

  def bill_date_human
    date = self.bill_date || Date.today
    date.strftime('%b %d, %Y')
  end
  def bill_date_human=(date_string)
    self.bill_date = Date.strptime(date_string, '%b %d, %Y')
  end

This performs great for my needs, but I want to do the same thing for several other date attributes… how would I take advantage of method missing so that any date attribute can be set/get like so?

  • 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-28T04:52:01+00:00Added an answer on May 28, 2026 at 4:52 am

    As you already know signature of desired methods it might be better to define them instead of using method_missing. You can do it like that (inside you class definition):

    [:bill_date, :registration_date, :some_other_date].each do |attr|
      define_method("#{attr}_human") do
        (send(attr) || Date.today).strftime('%b %d, %Y')
      end   
    
      define_method("#{attr}_human=") do |date_string|
        self.send "#{attr}=", Date.strptime(date_string, '%b %d, %Y')
      end
    end
    

    If listing all date attributes is not a problem this approach is better as you are dealing with regular methods instead of some magic inside method_missing.

    If you want to apply that to all attributes that have names ending with _date you can retrieve them like that (inside your class definition):

    column_names.grep(/_date$/)
    

    And here’s method_missing solution (not tested, though the previous one is not tested either):

    def method_missing(method_name, *args, &block)
      # delegate to superclass if you're not handling that method_name
      return super unless /^(.*)_date(=?)/ =~ method_name
    
      # after match we have attribute name in $1 captured group and '' or '=' in $2
      if $2.blank?
        (send($1) || Date.today).strftime('%b %d, %Y')
      else
        self.send "#{$1}=", Date.strptime(args[0], '%b %d, %Y')
      end
    end
    

    In addition it’s nice to override respond_to? method and return true for method names, that you handle inside method_missing (in 1.9 you should override respond_to_missing? instead).

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

Sidebar

Related Questions

I have several models with a date attribute and for each model I'd like
I have a model with several attributes, among them title and artist . The
I have an Auction model with several attributes, two of which are current_auction:boolean and
If I have Model.objects.all() I want to get only one object for any content_object=foo,
so i have a model with two date attributes # migration t.date :valid_from t.date
I have several existing XML schema files with a defined data model. I would
Lets say I have model inheritance set up in the way defined below. class
I have several models: Email Letter Call All three belong to a model Campaign.
I have one model, and 3 different forms that use this model (of course,
I have a model like so: class RunnerStat(models.Model): id_card= models.CharField(max_length=32) miles = models.PositiveSmallIntegerField() last_modified

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.