1

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

但我还没有完全理解 meta_search 相对于 searchlogic 的用法。

如果我有一个带有 :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 的用法!

参考:
meta_search https://github.com/ernie/meta_search

4

2 回答 2

1

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

编辑

如此 SO thread所述,通过以下方式安装:

  gem 'rd_searchlogic', :require => 'searchlogic', :git => 'git://github.com/railsdog/searchlogic.git'
于 2010-12-01T00:38:24.380 回答
1

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

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

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

于 2010-11-30T12:47:35.553 回答