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

  • Home
  • SEARCH
  • 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 45287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:47:07+00:00 2026-05-10T15:47:07+00:00

I have a few models that need to have custom find conditions placed on

  • 0

I have a few models that need to have custom find conditions placed on them. For example, if I have a Contact model, every time Contact.find is called, I want to restrict the contacts returned that only belong to the Account in use.

I found this via Google (which I’ve customized a little):

def self.find(*args)   with_scope(:find => { :conditions =>  'account_id = #{$account.id}' }) do     super(*args)   end end 

This works great, except for a few occasions where account_id is ambiguous so I adapted it to:

def self.find(*args)   with_scope(:find => { :conditions =>  '#{self.to_s.downcase.pluralize}.account_id = #{$account.id}' }) do     super(*args)   end end 

This also works great, however, I want it to be DRY. Now I have a few different models that I want this kind of function to be used. What is the best way to do this?

When you answer, please include the code to help our minds grasp the metaprogramming Ruby-fu.

(I’m using Rails v2.1)

  • 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. 2026-05-10T15:47:07+00:00Added an answer on May 10, 2026 at 3:47 pm

    You don’t tell us which version of rails you are using [edit – it is on rails 2.1 thus following advice is fully operational], but I would recommand you use the following form instead of overloading find yourself :

    account.contacts.find(...)  

    this will automatically wrap the find in a scope where the user clause is included (since you have the account_id I assume you have the account somewhere close)

    I suggest you check the following resources on scopes

    • http://ryandaigle.com/articles/2008/3/24/what-s-new-in-edge-rails-has-finder-functionality (this is not edge anymore 🙂 )
    • http://ryandaigle.com/articles/2008/8/20/named-scope-it-s-not-just-for-conditions-ya-know
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Wait, so you are creating a div, using position relative… May 11, 2026 at 3:22 pm
  • added an answer Swing has support for entering dates into text fields using… May 11, 2026 at 3:22 pm
  • added an answer My bad ... I should have used the Quick Promote May 11, 2026 at 3:22 pm

Related Questions

I want to create a product catalog that allows for intricate details on each
I have a substantial body of source code ( OOFILE ) which I'm finally
I would like to confirm that the following analysis is correct: I am building
I have a large c# solution file (~100 projects), and I am trying to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.