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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:17:49+00:00 2026-05-17T15:17:49+00:00

Note: I’m using Rails 2.3.8, not 3. I have a Photo model with a

  • 0

Note: I’m using Rails 2.3.8, not 3.

I have a Photo model with a default_scope:

default_scope :conditions => ["published = ?", true], :order => :position

Calling photo_album.photos returns all published photos ordered by position as it should. However, when looping through these photo albums in an admin panel to display the number of photos in each, the results are wrong:

pluralize(photo_album.photos.count, "photo")

returns 0, as none are published.

I know that similar questions have been asked, and the reply typically is along the lines of “use a with_exclusive_scope class method”. As far as I can tell, this completely prevents the use of standard Rails associations – basically resulting in something like this:

pluralize(Photo.all_photos_in_album(photo_album.id).count, "photo")

and requiring a class method like:

def Photo.all_photos_in_album(album_id)
  self.with_exclusive_scope { find(:all, :conditions => ["photo_album_id = ?", album_id]) }
end

just to display the total number of photos in an album. This seems insane – overriding the default should not require abandoning Rails association conventions. with_exclusive_scope can not be used in an instance method (protected method) either – this would have allowed me to create a PhotoAlbum instance method called “all_photos” to at least preserve the semblance of associations (photo_album.all_photos). But no, that is not allowed 🙁

Aside from removing default_scopes, which have proven very useful across the site, does anyone know of a way override default scopes and maintain Rails association syntax?

Thanks!

Edit:

I wound up adding a PhotoAlbum instance method that, while it isn’t an actual default_scope override, does at make for much nicer syntax in my views:

def all_photos_count
  PhotoAlbum.count_by_sql("SELECT COUNT(id) FROM photos WHERE photo_album_id = #{self.id} ORDER BY created_at")
end

pluralize(photo_album.all_photos_count, "photo")

While it’s not exactly an AR has_many association and it relies on pure SQL, it’s the best compromise I’ve found so far.

  • 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-17T15:17:50+00:00Added an answer on May 17, 2026 at 3:17 pm

    For me, the best solution in this case was to use neither default_scope nor has_many conditions and to just stick with named_scopes. Two named_scopes accomplished most of what I needed to keep my views lean:

    named_scope :public_list, :conditions => ["published = ?", true], :order => :position
    named_scope :private_list, :order => "created_at DESC"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.