How can I stop meta_search from defining a search on my models?

I use active_admin and bring meta_search to my project. (Which I do not want to use for anything else).

It seems that the definition of the search method on all my models, which then means when I turn on the tires, I can not use its search method.

There seems to be something strange in the way he defines a method as well - method_defined? says the search method is not defined, but when I call it, I get meta_search. Even if I define my own search method in the class when I call Document.search, I still get meta_search.

EDIT: I would be interested in general ways to solve this kind of thing. I solved this problem with Model.tire.search (since the bus is also available that way), but I still hate that the gem I donโ€™t even use can force me to use the workaround in the rest of my project.

EDIT: I don't know how to include code in the answers, so I will put this here.

# Meta_search loaded, tire is not
1.9.3p125 :001 > require "tire"   #=> true
1.9.3p125 :002 > Document.send(:include, Tire::Model::Search)
=> Document(...)
1.9.3p125 :003 > Document.search
  Document Load (2.1ms)  SELECT "documents".* FROM "documents" 
  # I get meta_search, as I should


# Tire loaded (and the include Tire::Model::Search is inside the class definition), meta_search is not loaded
1.9.3p125 :001 > Document.search
# I get tire, as I should
1.9.3p125 :002 > require "meta_search"   #=> true
1.9.3p125 :003 > Document.search
# I still get tire, all is well

# Tire loaded, meta_search is not loaded
1.9.3p125 :001 > require "meta_search"   #=> true
1.9.3p125 :002 > Document.search
  Document Load (1.8ms)  SELECT "documents".* FROM "documents" 
# I get meta_search, even though Document.search was already defined!

# Tire loaded, meta_search is not loaded, RAILS_ENV="production"
Loading production environment (Rails 3.2.2)
1.9.3p125 :001 > require "meta_search"
=> true 
1.9.3p125 :002 > Document.search
# I get tire!

My interpretation of this is that there is an error related to the way meta_search detects that a search is already defined when the class is not actually loaded. Hooray!

+3
source share
2 answers

corresponding 2 lines:

https://github.com/ernie/meta_search/blob/master/lib/meta_search.rb#L55

https://github.com/ernie/meta_search/blob/master/lib/meta_search/searches/active_record.rb#L46

, , , .

3 . "meta_search", "" ActiveRecord::Base. Document, , , , Tire, metasearch.

( 4), 2 , Tire . .

, , , . , gem.

# application.rb
# ...
Bundler.require(:default, Rails.env) if defined?(Bundler)
# now move search out of the way
ActiveRecord::Base.instance_eval { undef :search }

, , , .

, , . , : , :

class ActiveRecord::Base
  def self.search(*args, &block)
    if respond_to?(:tire)
       tire.search(*args, &block)
    else
       metasearch(*args, &block)
    end
  end
end

?

+6

, : active_admin Gemfile. meta_search, search, , , .

, , meta_search (, Rails), ActiveRecord:: Base.search, tire.search:

method_defined? , , search . , , - Document.methods.include?(:search). , search, (, def self.search)?

, " " - Ruby, . , , , , " ".

+1

All Articles