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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:46:36+00:00 2026-05-14T03:46:36+00:00

Hey! Maybe I am getting the idea of a subclass wrong, but I have

  • 0

Hey! Maybe I am getting the idea of a subclass wrong, but I have a Person model and it has an attrib called “age” so

Person.first.age #=> '20'

Now I want to have a model that’s basically persons 55 or older so I know I can have a class like this:
class Senior < Person
end

But how can I “pre-filter” the Senior class so that every object belonging to that class has age >= 55?

Senior.first.age #=> 56

UPDATE1:
So say I have Company has_many people, and Person belongs_to Company, so
Company.first.people #=> [“Jack”, “Kate”]

If Jack’s age is > 55, will it work then:
Company.first.seniors #=> “jack”

Or

Company.first.people.senior(s) #=> “jack”?

I know that named_scope might be what I want, but I also notice that named_scope seems to be a method on the Class variable Person. Not its instances, which does make sense to me. — So if I were to devise such a convenience filter for a collection of activerecord models (objects of the same class), how do I go about it? I am guessing I’d have to use a “detect” for such an array, but where will this go inside the Model’s definition?

Update 2
I am quite sure I haven’t been clear, so example
Want: first company’s 55 or older people
Company.first.people.detect{|p| p.age > 54}

I know this isn’t very long, but my conditions will go farther than just > 54 and it becomes clumsy to do this detect each time.

Thanks!

  • 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-14T03:46:37+00:00Added an answer on May 14, 2026 at 3:46 am

    You can use named scopes

    class Person < ActiveRecord::Base
      named_scope :seniors, :conditions => ['age >= ?', 55]
    end
    
    Person.seniors.first.age #=> 83
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I have a maybe very simple problem using the PreLoadFcn in my model.
hey guys having this really simple problem but cant seem to figure out have
Hey guys, I thought this would be fairly straightforward, but it's not it seems..maybe
hey guys, maybe some of you have experience with programming wordpress plugins. I have
Hey. So I have a Rails app that's getting deployed to a production machine
Hey I made a scaffold called pictures, but now I need to it to
Hey guys. This is a very simple question, I'm sure, but I'm getting myself
Hey all, title may be abit misleading but i didnt know the correct way
Hey i have been looking around and i cant find a way to get
*Hey , I have finished developing my app In my iphone apps I have

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.