Skip to content

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

@paulschreiber

Description

@paulschreiber

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 16e7e26.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions