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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:43:18+00:00 2026-05-16T11:43:18+00:00

I think it’s safe to say everyone loves doing something like this in Rails:

  • 0

I think it’s safe to say everyone loves doing something like this in Rails:

Product.find(:all, :conditions => {:featured => true})

This will return all products where the attribute “featured” (which is a database column) is true. But let’s say I have a method on Product like this:

def display_ready?
     (self.photos.length > 0) && (File.exist?(self.file.path))
end

…and I want to find all products where that method returns true. I can think of several messy ways of doing it, but I think it’s also safe to say we love Rails because most things are not messy.

I’d say it’s a pretty common problem for me… I’d have to imagine that a good answer will help many people. Any non-messy ideas?

  • 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-16T11:43:19+00:00Added an answer on May 16, 2026 at 11:43 am

    The only reliable way to filter these is the somewhat ugly method of retrieving all records and running them through a select:

    display_ready_products = Product.all.select(&:display_ready?)
    

    This is inefficient to the extreme especially if you have a large number of products which are probably not going to qualify.

    The better way to do this is to have a counter cache for your photos, plus a flag set when your file is uploaded:

    class Product < ActiveRecord::Base
      has_many :photos
    end
    
    class Photo < ActiveRecord::Base
      belongs_to :product, :counter_cache => true
    end
    

    You’ll need to add a column to the Product table:

    add_column :products, :photos_count, :default => 0
    

    This will give you a column with the number of photos. There’s a way to pre-populate these counters with the correct numbers at the start instead of zero, but there’s no need to get into that here.

    Add a column to record your file flag:

    add_column :products, :file_exists, :boolean, :null => false, :default => false
    

    Now trigger this when saving:

    class Product < ActiveRecord::Base
      before_save :assign_file_exists_flag
    
    protected
      def assign_file_exists_flag
        self.file_exists = File.exist?(self.file.path)
      end
    end
    

    Since these two attributes are rendered into database columns, you can now query on them directly:

    Product.find(:all, :conditions => 'file_exists=1 AND photos_count>0')
    

    You can clean that up by writing two named scopes that will encapsulate that behavior.

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

Sidebar

Related Questions

Think about doing this: import matplotlib.pyplot as plt plt.plot(x_A,y_A,'g--') plt.plot(x_B,y_B,'r-o') plt.show() How would you
Think I have an integer array like this: a[0]=60; a[1]=321; a[2]=5; now I want
I think everyone knows this site http://pinterest.com/ and I don't want to create site
Think that I have many activities,and all I want is this: I have a
Think about a speedometer, and imagine all these little strokes around it like 10
Think about a table like this ID Value 100 1 100 3 101 1
think about an array like this: ... key1 => some_call(val1, $params), key2 => some_call(val1,
Think: tiling my emacs window with eshells, a la xmonad. Is this possible? I
I think I've figured out the problem. I'm using a IP webcam stream, doing
I think I have a basic understanding of this, but am hoping that someone

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.