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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:49:45+00:00 2026-05-24T20:49:45+00:00

I have a method in my class that takes a parameter that’s a number.

  • 0

I have a method in my class that takes a parameter that’s a number. It looks something like this:

def with_at_least_x_posts(min_posts)
    self.where("posts_counter >= ?", min_posts)
end

I’d like to make a method that takes that parameter inside its name, rather than in parenthesis, so instead of calling

User.with_at_least_x_posts(10)

I can call

User.with_at_least_10_posts

This would require that method to be defined via some sort of regular expression mechanism. I know that the find_by method works that way (i.e. find_by_some_column), so it should be possible?

Could someone save me some time by telling me how to achieve that in Ruby 1.9.2 and Rails 3.1 without digging into the Rails core and finding out for myself?

Thanks!


Update: while waiting for an answer, I’ve been digging into the Rails core. Seems like they’re overriding the method_missing method in ActiveRecord::Base and delegating the handling of custom methods to a module called DynamicFinderMatch. Interesting stuff!
Would be REALLY nice to be able to define methods in Ruby like this:

def /with_at_least_[0-9]+_posts/
   x = $0
end

but I guess that’s not really possible at this point in time. Interesting stuff!

  • 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-24T20:49:46+00:00Added an answer on May 24, 2026 at 8:49 pm

    You should define a call within the general Ruby method method_missing

     def with_at_least_x_posts(min_posts)
       self.where("posts_counter >= ?", min_posts)
     end
    
     def method_missing(sym, *args, &block)
       begin
         if sym.to_s =~ /with_at_least_(\d+)_posts/
           return send :with_at_least_x_posts, *(args.push($1.to_i)), &block
         end
       rescue NoMethodError => e
         raise "Unable to find method: #{e.inspect}"
       end
    
       return super
     end
    

    From here, you’ll be able to call myObj.with_at_least_10_posts. Give it a go! You can read more here.

    P.S. This is common in Ruby (it’s what ActiveRecord does). Beware that if you get a StackOverflow error, it’s because a method_missing error raised in method_missing will lead to an infinate recursion. Be cautious!

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

Sidebar

Related Questions

I have a static method that takes a parameter and returns a class. the
There is a generic method that takes a class as parameter and I have
I have a fairly simple class that looks like this: class Person { public:
I have a method that takes a list of entities ( Class es) and
I have a Filter Method in my User Class, that takes in a list
I have a problem with my database class. I have a method that takes
i have something like this class A { virtual void doBla(A *a) = 0;
Assuming I have a class with a method that takes a System.Linq.Expressions.Expression as a
I have an action method that takes in a string as its only parameter.
I have a repository with an Add method that takes an IEnumerable as parameter:

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.