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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:49:28+00:00 2026-05-26T13:49:28+00:00

I am trying to set my own method to override a database property, so

  • 0

I am trying to set my own method to override a database property, so that I can intercept the value where the database value is null.

Rails seems to allow me to set a method with the same name as a database column, however I would need an alternative way to access the database value to return what I want.

Imagine I have a database column “date”, and I want to set that to something else when it is null in the database.

I would like to set a method as below

def date
 if (alternative_db_value_object).date == nil
  "Put this"
 else
  "Put that"
 end
end

Alternatively I would greatly appreciate if anyone could suggest a better way of doing this.

  • 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-26T13:49:29+00:00Added an answer on May 26, 2026 at 1:49 pm

    .. within the ‘date’ method, you can either use

    self[:date]
    

    or

    read_attribute(:date)
    

    UPDATE: it seems this doesn’t work anymore on rails 5. One is supposed to use super:

    def length=(minutes)
      super(minutes.to_i * 60)
    end
    
    def length
      super / 60
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement my own kind method cache. For that, first I want
I'm trying to set up a custom backend that queries another database, for which
In Qt I'm trying to set up my own QWidget so everything should work
I'm trying to set up a site which allows users to create their own
I'm trying to figure out how one would set up a database and/or model
I am getting the EXC_BAD_ACCESS error when trying to set a value inside a
I'm working with DelayedJob and I need to override a method that I thought
I'm trying to implement my own progress bar. I have a constructor, that inits
I'm trying to override a class derived from Activity (called NativeActivity) so I can
Im trying to create my own custom tag so i can display or hide

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.