views:

163

answers:

2

I'd like to use searchlogic's scope_procedure feature like so

class MyModelObject < ActiveRecord::Base
   scope_procedure :my_scope_proc, lambda { |p1, p2| { :conditions => "p1 >= #{p1} AND p2 < #{p2}" }}
end

Then, I am doing the search:

scope = MyModelObject.search(:my_scope_proc => true)
scope.all

The above code obviously doesn't work because I didn't pass p1 and p2 parameters to my named scope.

I can't figure out how to pass parameters to the named scope.

+1  A: 

A wild guess is (meaning, I didn't check!):

scope = MyModelObject.search(:my_scope_proc => [p1, p2])
scope.all

One suggestion for the scope_procedure:

class MyModelObject < ActiveRecord::Base
  scope_procedure :my_scope_proc, lambda { |p1, p2| { :conditions => ["p1 >= ? AND p2 < ?", p1, p2] }}
end

This prevents SQL injection.

Or the searchlogic way:

class MyModelObject < ActiveRecord::Base
  scope_procedure :my_scope_proc, lambda { |p1, p2| p1_gte(p1).p2_lt(p2) }
end
Aaron Qian
Thanks very much for your suggestion. Although it didn't work as is, it led me to a solution. It works with one parameter and only the searchlogic way. I ended up passing an array of parameters. Not the most readable code or elegant solution but it works.
Greg
A: 

I was able to make it work with one parameter as follows:

class MyModelObject < ActiveRecord::Base
  scope_procedure :my_scope_proc, lambda { |p| p1_gte(p[0]).p2_lt(p[1]) }
end

scope = MyModelObject.search(:my_scope_proc => [p1, p2])
scope.all
Greg