开发者

Passing arguments to scope_procedure in searchlogic

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 p开发者_运维问答1 and p2 parameters to my named scope.

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


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜