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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:16:01+00:00 2026-05-27T04:16:01+00:00

I have a class but I want some customized behavior if a field is

  • 0

I have a class but I want some customized behavior if a field is of a particular value. Otherwise I want the default behavior.

class Foo
  include Mongoid::Document

  belongs_to :parent, :foreign_key => "parent_id", :class_name => "Pad"
  has_many :children, :foreign_key => "parent_id", :class_name => "Pad"
  field :bar, :type => String

  def children
    if self.bar == "some value"
      # Do something special
    else
      return self.children   # <- What goes here that isn't an infinite loop?
    end
  end
end

What should the else branch be?

I do not want to reimplement children so I’m looking for more than just Foo.where(:parent_id => self.id)

  • 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-27T04:16:02+00:00Added an answer on May 27, 2026 at 4:16 am

    Active Support provides a method to keep the original method around for cases like this — it’s called “alias_method_chain”… It’s a bit of black magic, but you define the {method}_with_{condition} and call the original with {method}_without_{condition}

    In your case: children_with_bar and you get to the original by calling children_without_bar

    require 'active_support'
    require 'mongoid'
    
    class Foo
      include Mongoid::Document
      has_many :children, :foreign_key => "parent_id", :class_name => "Pad"
      field :bar
    
      def children_with_bar
        if self.bar == "some value"
          return "yooooo"
        else
          children_without_bar
        end
      end
    
      alias_method_chain :children, :bar
    end
    
    
    Foo.new(:bar=>"some value").children
     => "yooooo" 
    Foo.new(:bar=>"different").children
     ## Will run original children method
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with some abstract methods, but I want to be able
I have derived a class BitmapDrawable in android for some reason but I want
I have a class that is mapped in fluent nhibernate but I want one
I have a generic class, but I want my type to be forced to
I have a class that I need to perform some actions on but I
I have a class that acts as a wrapper to Smarty but want to
I have a class which does operations on the database, but I want to
I have some class with associated list. I want this list not to be
i neeed something like this in C#.. have list in class but decide what
I have the points by the end of the GenerateButton class but now that

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.