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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:41:43+00:00 2026-05-23T21:41:43+00:00

I have a Person model, which includes a property representing the data of birth

  • 0

I have a Person model, which includes a property representing the data of birth (birth_date).

I also have a method called age(), which works out the current age of the person.

I now have need to run queries based on the person’s age, so I have replicated the logic of age() as a computed column in MySQL.

I cannot workout how I would make this additional column part of the default select statement of the model.

I would like to be able to access the age as if it were a native property of the Person model, to perform queries against it and access the value in my views.

Is this possible, or am barking up the wrong tree?

I thought I might be able to define additional fields through default_scope or scope, but these methods seem to only recognise existing fields. I also tried default_scope in tandem with attr_assessor.

Possible workarounds I’ve considered but would prefer not to do:

  • Create an actual property called age and populate through the use of callbacks. The date is always changing, so this obviously would be be reliable.

  • Replicate the logic in ActiveRecord as age() and in a scope as a where cause. This would achieve what I need, but doesn’t feel very DRY.

  • I am already caching the results of the age() method. it is the ability to use the field in where clauses that I am most interested in.

There must be a way to define dynamic fields through SQL that I can access through the model by default.

Any help would be appreciated.

Rich

UPDATE

An example of my failed attempt to utilise scopes:

default_scope :select => "*, 2 as age"

attr_accessor :age

age is blank, I assume because scopes only deal with limiting, not extending.

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

    kim3er your solution to your problem is simple. Follow these steps:

    Loose the attr_accessor :age from your model. You simply don’t need it.

    Leave the default scope at the Person model: default_scope :select => "*, 2 as age"

    Lastly open up a console and try

    p = Person.first
    p.age
    => 2
    

    When you define a select using as, Rails will automagically add those methods on the instances for you! So the answer to your question:

    There must be a way to define dynamic fields through SQL that I can access through the model by default.

    is:

    Rails

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

Sidebar

Related Questions

Say I have a model object 'Person' defined, which has a field called 'Name'.
I have a Rails model called Person which has database table columns for first_name
I have a model in my rails application which is class Person < ActiveRecord::Base
In my core data model I have a Person entity that has a to
I have a model called Person. I want User to inherit from Person. class
Hay, I have a Model which looks like this class Person(models.Model): name = models.CharField(blank=False,
I have a data model that includes element types Stage, Actor, and Form. Logically,
I have a Model which contains an Address and Person twice, once for the
Suppose I have a model Person which has a one to many relationship with
I have a Person model which have a relationship with a City model: class

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.