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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:59:11+00:00 2026-05-24T19:59:11+00:00

Foo has_many Bar . I want to do something like: foo_instance.bars.find_with_custom_stuff(baz) How do I

  • 0

Foo has_many Bar. I want to do something like:

foo_instance.bars.find_with_custom_stuff(baz)

How do I define find_with_custom_stuff so that it’s available on the bars relation? (and not just a Bar class method?)

update

I want to do something more complicated than a scope. something like:

def find_with_custom_stuff(thing)
  if relation.find_by_pineapple(thing)
    relation.find_by_pineapple(thing).monkey
  else
    :banana
  end
end
  • 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-24T19:59:13+00:00Added an answer on May 24, 2026 at 7:59 pm

    Scopes, scope in rails 3 and named_scope in rails 2.

    class Bar
      scope :custom_find, lambda {|baz| where(:whatever => baz) }
    end
    
    foo_instance.bars.custom_find(baz)
    

    scope should return a scope, so given your update you probably don’t want to use scope here. You can write a class method though, and use scoped to access the current scope, like:

    class Bar
      def self.custom_find(thing)
        if bar = scoped.find_by_pineapple(thing)
          bar.monkey
        else
          :banana
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ActiveRecord model class Foo that has_many Bar . I want to
Given the following: Foo has_many :bars, :through => :baz and Foo accepts_nested_attributes_for :bar I
I have a model Foo that has_many 'Bar'. I have a factory_girl factory for
Let's say I have models that look like this: class Foo < ActiveRecord::Base has_many
Given the following definitions Foo has_many :bars, :dependent => :nullify, :autosave => false Bar
I have models foo, bar, and baz like so # has a field join_condition_string
I have two classes, Foo and Bar. Foo has_many Bars. Bar is actually the
I have a model with nested attributes : class Foo < ActiveRecord::Base has_many :bar
I have a base class Foo that has an Update() function, which I want
I have a class Foo which has_many Bars. Foo has an attribute, some_id. I

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.