binarylogic / searchlogic

Searchlogic provides object based searching, common named scopes, and other useful tools.
http://rdoc.info/projects/binarylogic/searchlogic
MIT License
1.4k stars 133 forks source link

HABTM relationship queries fail with "undefined method" errors #165

Open paulschreiber opened 10 years ago

paulschreiber commented 10 years ago

In SearchLogic 2.5.9 or greater, HABTM relationship queries fail with "undefined method" errors.

undefined method `bar_id_equals_any' for #<Class:0x7febd2642d30>
/home/myapp/shared/bundle/ruby/1.8/gems/searchlogic-2.5.14/
lib/searchlogic/named_scopes/column_conditions.rb:81:in `method_missing'

The cause of this is the change from respond_to? to respond_to_missing? in https://github.com/binarylogic/searchlogic/commit/16e7e2605a605dc39d8db0734f69dd9d1e1f1872.

This may work in Ruby 1.9+, but fails in 1.8.7 (with Rails 2.3.18).

Example

class Foo
  has_and_belongs_to_many :bars
end
class Bar
  has_and_belongs_to_many :foos
end

Foo.bars_id_in(2)    # fails

I worked around this by monkey patching:

module Searchlogic
  module NamedScopes
    module ColumnConditions

      # In SearchLogic 2.5.9+, this was changed to respond_to_missing?
      # which breaks Foo.bar_id_equals_any(...) where Foo habtm Bar
      def respond_to?(*args)
        super || (self != ::ActiveRecord::Base && !self.abstract_class? && !create_condition(args.first).blank?)
      end

    end
  end
end