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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:24:51+00:00 2026-06-01T14:24:51+00:00

Consider a model user: User(id: integer, name: string, email: string, status: string) When I

  • 0

Consider a model user:

User(id: integer, name: string, email: string, status: string)

When I built the application, status was a field in the database. But new requirements means a change so that status is dynamically calculated in some cases. For example, on Sundays, if their last name starts with A, then their status is “not ok”.

I want to encapsulate this functionality in the User model with something like the following:

Class User < ActiveRecord::Base

  def status
    if is_sunday && last_name.starts_with('A')
        return 'not ok'
    else
       return status
    end
  end

end

What is the best way to do this? If the above code does work, it seems bad practice to override the field in the database with a method.

Alternatively, I could create a method get_status to use the above code. But in this case I need to change every reference to status throughout the application. That doesn’t sound good.

  • 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-01T14:24:53+00:00Added an answer on June 1, 2026 at 2:24 pm

    I’ve seen in the ActiveRecord::Base documentation that you can override default accessors provided by ActiveRecord. Of course you can’t use status as you do because it makes the method somewhat recursive.

    You can access the column value in the DB by using read_attribute(:attribute_name) or self[:attribute_name] if you prefer. So long story short, your accessor becomes:

    Class User < ActiveRecord::Base
      def status
        if is_sunday && last_name.starts_with('A')
          'not ok'
        else
          read_attribute(:status)
        end
      end
    end
    

    UPDATE: I’ve tried it and it works but pay attention because if you don’t call this method explicitly you get the DB content…for example you try to query the users table to find users with status = "not ok" you could have surprises.

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

Sidebar

Related Questions

consider scenario, User model with name, birth_date fields (here birth_date is optional) inside view
Consider for instance my following database model. UserTable(Id, Username) TextContentTable(Id, Name, Description, CreatedByUserId, ModifiedByUserId,
Consider a model without an initialize method, defined as follows: class User < ActiveRecord::Base
Let's consider this model class Session(models.Model): tutor = models.ForeignKey(User) start_time = models.DateTimeField() end_time =
Consider the following Django models: class Host(models.Model): # This is the hostname only name
Consider the models: Player name Text nick Text email Text Maybe phone Text Maybe
Our application is built in VB6 and delivered in a SaaS model via Citrix.
Consider a Name model which has a required label attribute and an arbitrary Rails
Consider these 3 models: # models.py class City(models.Model): name = models.CharField(max_length=50) class Institute(models.Model): name
Model Member belongs_to Discipline , i.e. user can have a discipline but it is

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.