rails 3 meta_search用法

时间:2010-11-30 02:53:21

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

我正在将我的应用从Rails2升级到Rails3 我的Rails2应用程序大量使用searchlogic 谷歌搜索后,我发现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的用法!

价:
meta_search https://github.com/ernie/meta_search

2 个答案:

答案 0 :(得分:1)

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

@search = User.search(:user_name_eq =&gt;“Bob”)

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

答案 1 :(得分:1)

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

修改

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

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