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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:40:59+00:00 2026-05-22T21:40:59+00:00

I realize this is not necessarily the smartest way to do this, but now

  • 0

I realize this is not necessarily the smartest way to do this, but now my curiosity is active and I am curious how to do it.

I have a model in the Rails project. We’ll call it Deal. Per ActiveRecord and all that cool stuff there are columns defined in the database like UPDATED_AT and those become methods on Deal: deal.updated_at => ’04/19/1966 3:15am’

Say I wanted to instead have methods that told me the day of the week rather than the whole date and time thing. I realize there are methods ON the DateTime class so I can do

  deal.updated_at.day_of_week => 'Monday' (*)

but what if I just wanted

  deal.updated_day => 'Monday'

I can write in deal.rb

  def update_day
    self.updated_at.day_of_week
  end

Got it.

But what if I wanted it to ALWAYS have the method available for ANY date column that was added to the model?

I saw define_method out there (some here on StackOverflow). So I understand that. But I would want to call it right after ActiveRecord did its magic, right? So if my Deal model had updated_at, created_at, offered_at and lawsuit_at I would want matching methods for each one. More importantly, if another developer came and added a column called scammed_at I would want scammed_day created along with the scammed_at method.

How would I do that?

Thanks.

(*) Uh, or something like that, I always look that call up.

  • 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-22T21:41:00+00:00Added an answer on May 22, 2026 at 9:41 pm

    I guess something like the following should do the trick. In your model:

    # looping through all model's columns
    self.columns.each do |column|
      #if column's name ends with "_at"
      if column.name =~ /_at$/
        #create method like "udpated_day" 
        define_method "#{column.name[0..-4]}_day" do
          self.send(column.name).day_of_week
        end
      end
    end
    

    But it implies every column has a valid day_of_week method…

    Well you get the idea I think. Don’t hesitate to ask for details

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

Sidebar

Related Questions

I realize this question has probably been asked numerous times, but I have not
I realize this is a basic question but I have searched online, been to
I realize this sounds a little crazy, but I'm working on a project for
Before anything, this is not necessarily a question.. But I really want to know
So, I realize this might not be the right site, but I didn't think
I just realized that this piece of code works well in Firefox but not
I realize this is syntactically bad but I figure it somewhat explains what I'm
I realize this is more of a hardware question, but this is also very
I realize this is a rather odd request, but I was wondering if anyone
I realize this is probably a hopelessly newbie question, but what is the difference

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.