【问题标题】:rails 3 meta_search usagerails 3 meta_search 用法
【发布时间】:2011-05-17 15:27:00
【问题描述】:

我正在将我的应用程序从 Rails2 升级到 Rails3。
我的 Rails2 应用大量使用了搜索逻辑。
谷歌搜索后,我知道 searchlogic 与 Rails3 不兼容,需要改用 meta_search。

但我还没有完全理解 meta_search 相对于搜索逻辑的用法。

如果我有一个带有 :name 和 :address 字段的用户模型,我无法将以下方法与 meta_search 一起使用。我究竟做错了什么?

ruby-1.9.2-p0 > User.name_null  
NoMethodError: undefined method `name_null' for #<Class:0x000000038d5ce0>
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:in `method_missing'
from (irb):7
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'  
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

像 User.user_id_eq(1) 或 User.name_equals("Blah") 这样的方法都不起作用。 我想我还没有理解 meta_search 的用法!

参考:
元搜索https://github.com/ernie/meta_search

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 meta-search


    【解决方案1】:

    密切关注rd_searchlogic,它看起来与 Rails 3 兼容,但在撰写本文时仍是预览版。

    编辑

    this SO thread 中所述,通过以下方式安装:

      gem 'rd_searchlogic', :require => 'searchlogic', :git => 'git://github.com/railsdog/searchlogic.git'
    

    【讨论】:

    • 我也试过rd_searchlogic,同样的问题`method_missing'。
    • Rails3 正在快速经历很多转变,尤其是“arel”。由于它破坏了 searchlogic 和vestal_versions,我现在不知何故感到不舒服。我想我会等一段时间,直到一个稳定的核心和相应的插件可用,就像我们拥有的精彩 Rails 2.3.5 版本一样。
    【解决方案2】:

    方法是要在 FormBuilder 中设置的属性。因此,您需要调用 user_name_equals = "Bob",而不是 user_name_equals("Bob")。此外,它们将位于搜索实例上,而不是模型本身。

    @search = User.search(:user_name_eq => "Bob")

    如果您正在寻找可以在日常查询构建中使用的东西,请改用 MetaWhere。 http://metautonomo.us/projects/metawhere

    【讨论】:

    • 感谢 Ernie 的回复以及在 Rails3 中搜索的好插件。但是使用 meta_where 我也没有得到 searchlogicy 语法。我想在 meta_where 上使用某种包装器方法会很好,这些方法展示了搜索逻辑语法,以实现从 Rails2 到 Rails3 的平滑迁移。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    相关资源
    最近更新 更多