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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:28:25+00:00 2026-05-25T15:28:25+00:00

I use the following method_missing implementation to give a certain model an adaptable named_scope

  • 0

I use the following method_missing implementation to give a certain model an adaptable named_scope filtering:

class Product < ActiveRecord::Base
  def self.method_missing(method_id, *args)

    # only respond to methods that begin with 'by_'
    if method_id.to_s =~ /^(by\_){1}\w*/i

      # extract column name from called method
      column = method_id.to_s.split('by_').last

      # if a valid column, create a dynamic named_scope
      # for it. So basically, I can now run
      # >>> Product.by_name('jellybeans')
      # >>> Product.by_vendor('Cyberdine')
      if self.respond_to?( column.to_sym )
        self.send(:named_scope, method_id, lambda {|val|
          if val.present?
            # (this is simplified, I know about ActiveRecord::Base#find_by_..)
            { :conditions => ["#{base.table_name}.#{column} = ?", val]}
          else
            {}
          end
        })
      else
        super(method_id, args)
      end
    end
  end
end

I know this is already provided by ActiveRecord::Base using find_by_<X>, but I’m trying to go a little bit beyond the example I’ve given and provide some custom filtering taylored to my application. I’d like to make it available to selected models w/o having to paste this snippet in every model class. I thought of using a module and then mixing it in the models of choice – I’m just a bit vague on the syntax.

I’ve gotten as far as this when the errors started piling up (am I doing this right?):

module GenericFilter
  def self.extended(base)

    base.send(:method_missing, method_id, *args, lambda { |method_id, args|
    # ?..
    })

  end
end

Then I hope to be able to use it like so:

def Product < ActiveRecord::Base
  include GenericFilter
end

def Vendor < ActiveRecord::Base
  include GenericFilter
end

# etc..

Any help will be great – thanks.

  • 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-25T15:28:25+00:00Added an answer on May 25, 2026 at 3:28 pm

    Two ways of achieving this

     module GenericModule
       def self.included(base)
         base.extend ClassMethods
       end
    
       module ClassMethods
         def methods_missing
           #....
         end
       end
     end
    
     class YourModel 
       include GenericModule
       .. 
     end
    

    or

      module GenericModule
        def method_missing
          #...
        end
      end
    
      class MyModel
        extend GenericModule
      end
    

    I would suggest using the first one, its seems cleaner to me. And as general advise, I’d avoid overriding method_missing :).

    Hope this helps.

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

Sidebar

Related Questions

I use the following method to break the double loop in Python. for word1
How come when I use the following method, to be used to convert all
Using PHP's Image and GD functions you can use the following method to finally
I have converted my Datatable to json string use the following method... public string
I have a program that makes use of the following method to get a
I'm converting an application to use Java 1.5 and have found the following method:
Eg. I have following delegate method I want to use as a callback function
I have a method that I will use in the following contexts: 1. User
I wish to use the method with the following signature: exec(String command, String[] envp,
I am getting the following exception when i try to use the fetchall method

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.