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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:13:21+00:00 2026-06-08T21:13:21+00:00

I got a little problem to understand where should I add an method that

  • 0

I got a little problem to understand where should I add an method that all the models can have access to it. I read other similar posts but it’s not very clear where to add it. Some of them said about add it on “/lib” as a module an then include it in the model class (Already try this without luck). So what it’s the best practice for add this?

I’m trying the following:

My module on: /lib/search.rb

module Search     
   def self.search(params,columns_search)
        srch = params[:search]

        if srch.blank?
          scoped
        else
          search= []
          #Add conditions for the search
          columns_search.map do |column|
            search << (sanitize_sql_for_conditions ["LOWER(CAST(#{column} as TEXT)) LIKE ?", "%#{srch.downcase}%"])
         end

          where("(#{conditions.join(" and ")})")

    end
end

On my model cars.rb

class Cars < ActiveRecord::Base
  include Search

  attr_accessible :name

end

But i’m getting the following error on my console:

Started GET “/cars” for 127.0.0.1 at 2012-08-01 11:56:54 -0500

ActionController::RoutingError (uninitialized constant Car::Search):
app/models/car.rb:2:in `’

Any help will be appreciated! 🙂

  • 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-06-08T21:13:23+00:00Added an answer on June 8, 2026 at 9:13 pm

    The technique you mention seems like a reasonable approach – create a module (which will probably live in /lib) that defines the methods you want the models to have, and then include it in each model that needs it.

    For instance, my current project has Images, Videos and Audios, all of which have certain method that should be available to them because they’re all types of media.
    I define a module in lib/media.rb:

    module Media
      def self.included(base)
        # Associations etc. go here
        base.has_many :things
        base.mount_uploader :image, ImageUploader
        base.attr_accessible :image
        base.before_save :do_something
      end
    
      def do_something(argument=nil)
        #stuff
      end
    
    end
    

    And then in each of the media models, I include it:

    class Video < ActiveRecord::Base
      include Media
    
    end
    

    I can then call Video.first.do_something, just as though the method were defined in the model.

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

Sidebar

Related Questions

I got a little problem, I got a code, that reads all pixels and
I have a little problem that I don't understand. I have a db that
i got a little question, i just can't understand, what is the problem and
i`ve got a little problem with LINQ. I read out some information via XML-RPC.
I have got a little problem. I got a bxslider running on a website
I have got a little problem in IE8 with the nivoslider script and i
I have a little bit of a problem... I understand what a EXC_BAD_ACCESS error
I've got a little problem with correct query and hope you can help me.
I got a little problem. Sometimes, when I try to call the following code,
I got a little problem, it seems simple (personally I think it is), but

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.