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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:02:42+00:00 2026-06-17T16:02:42+00:00

I have a Camping which has one Author . An Author has many Campings

  • 0

I have a Camping which has one Author. An Author has many Campings.

Getting the “top 5”, defined as “5 last campings” is done with:

@author.campings.order(:created_at).limit(5)

But I’d like this concept if “top 5” to be moved into the Camping model, this seems cleaner to me; after all, the Camping is the only one who knows what top is.

Something like

@author.campings.top

But how to define this on Camping? @author.campings is not a Camping, but an ActiveRecord::Relation. So something like following in models/camping.rb does not work:

# Scope for "top" campings
def top(amount)
  self.order("created_at").limit(amount)
end

Used as

@author.top(5)

For now, I only need this “top” thing through the relation, @author in above-mentioned examples. For now, the definition of “top” is simply “last 5 campings”, but in a next iteration this will be more complex based on amount of views, ratings or other parameters and fields on Camping. Which leads me to believe that Camping is the correct place to define “top”, not Author.

I could add this to Author, but with my limited knowlegde that feels like a violation of the isolation pattern; Author suddenly needs to know about fields on Camping:

# Last 5 campings for this author
def top_campings(amount)
  campings.order("created_at").limit(amount)
end

Used as

@author.top_campings(5)

How are such relations normally solved?

  • 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-17T16:02:42+00:00Added an answer on June 17, 2026 at 4:02 pm

    You would want to define top in your Camping class this way:

    class Camping < ActiveRecord::Base
      def self.top(count)
        order(:created_at).limit(count)
      end
    end
    

    When you define it as a class method, it is available through the ActiveRecord::Relation, so you can call @author.campings.top(5)

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

Sidebar

Related Questions

Have deployed numerous report parts which reference the same view however one of them
I have made a application which reads information from an Api. Link : http://api.amp.active.com/camping/campground/details?contractCode=CO&parkId=50032&api_key=2chxq68efd4azrpygt5hh2qu
Have a linking (or ref) table which has a dual primary key. Need to
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have done quite a bit of searching for a guide (of any substance) for
Have got a method which returns IEnumerable<User> which I have been using Linq /
Have not done this before, so obviously I suck at it. Here 64 pixels
Have a simple iPhone app with a single UIViewController and two Views in one
I have a load of content as shown below: <td width=20%><a href=/search?tbs=shop:1&q=camping+lantern&sampleq=1>camping&nbsp;lantern</a></td> <td width=20%><a

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.