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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:18:56+00:00 2026-06-02T21:18:56+00:00

I recently discovered scopes (me == Rails noob). Cool. But named scopes are dead

  • 0

I recently discovered scopes (me == Rails noob). Cool. But named scopes are dead I find out. I wanna do it right, so I change this:

 def Foo
    scope :only_some, where('some conditions')

To this (as directed):

class << self

   def only_some
      where('some conditions')
   end

end

And then this stopped working:

bar.foos.only_some

Exception:

undefined method `only_some' for #<ActiveRecord::Relation:0x007fb398d99178>

What am I doing wrong? Should I keep using scopes?

  • 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-02T21:18:57+00:00Added an answer on June 2, 2026 at 9:18 pm

    On the contrary, scopes are preferred over class methods for querying associated objects because of two reasons:

    1. scopes simplify generation of complex queries. Think of scopes as ‘lazy’ sequences which get evaluated only when the caller needs the value (that is, in the end).
      So, you can construct a complex query by chaining scope methods without worrying about performance hit by multiple SQL queries.
      You can do so in class methods as well, but with less flexibility. You will have to use ‘scoped’ method in these class methods to act on the current scope.

    2. You can call scope methods on a collection of associated objects as well as on a single object. This is because ActiveRecord associations are instances of ActiveRecord::Relation class on which scope methods can be invoked whereas it is not the case with class methods. And this is precisely the problem you are facing. In your example, when you define ‘only_some’ as a scope method, ActiveRecord will invoke ‘only_some’ on ActiveRecord::Relation object which in turn will invoke that method on each of the foos. But when you redefine it to be a class method, you can call this method only on the model class Foo.

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

Sidebar

Related Questions

I recently discovered that 2 == [2] in JavaScript. As it turns out, this
I recently discovered this little scala example called Simple interpreter using monads : object
I recently discovered that the java.lang.String.substring method does not return a new string, but
I recently discovered the shorthand if statement and after searching online I couldn't find
I recently discovered that rails or some other entity is messing up my code
So I recently discovered this Source Mate tool for Flash builder with all the
I just recently discovered an INotifyPropertyChange interface. I managed to implement this interface in
Having recently discovered this method of development, I'm finding it a rather nice methodology.
I recently discovered NginX, and decided to try it out on my server. I
I recently discovered you can compile JScript into console apps using a built in

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.