将参数传递给 searchlogic 中的scope_procedure

发布于 2024-08-30 18:26:21 字数 456 浏览 9 评论 0原文

我想像这样使用 searchlogic 的 scope_procedure 功能

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

然后,我正在进行搜索:

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

上面的代码显然不起作用,因为我没有通过 p1 和 < code>p2 参数到我的命名范围。

我不知道如何将参数传递到命名范围。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埖埖迣鎅 2024-09-06 18:26:21

一个疯狂的猜测是(意思是,我没有检查!):

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

对scope_procedure的一个建议:

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

这可以防止SQL注入。

或者搜索逻辑方式:

class MyModelObject < ActiveRecord::Base
  scope_procedure :my_scope_proc, lambda { |p1, p2| p1_gte(p1).p2_lt(p2) }
end

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
够钟 2024-09-06 18:26:21

我能够使用一个参数来使其工作,如下所示:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文