开发者

Effective way of doing a rails search query

I'm a newbie to rails. I've done a search query

myresult = Model1.includes(':model2', ':model3').where("model1.field1 LIKE ? OR model1. field2 LIKE ? OR model2.field1 LIKE ? OR model2.field2 LIKE ? OR model2.field3 LIKE ? OR model3.field1 LIKE ? " value1, value1, value2, value2, value3, value4 ).select("model1.field1, ..., model1.fieldn, model2.field1.., model3.field2 ")

Now I hear this is not a good practice. Other one I found is someone said to use joins of Arel. But Arel seems to be relativel开发者_JAVA百科y new and for me it looked like SQL-injection prone. Can some one suggest a good way of doing this in ruby on rails.

Thank you Sai


If you're trying to do full-text search across several fields, you don't want to do this in SQL.

You should use a speciallized full text search system like Sphinx or Solr.

Check out Thinking Sphinx (Rails Sphinx API):

http://freelancing-god.github.com/ts/en/


You are fighting the framework. Arel is the way to go in order to do the query the Rails way which is your question.

Arel is not SQL-injection prone when you use it properly. You need to check out this page: http://guides.rubyonrails.org/active_record_querying.html

Create some scopes in your model and chain them together to create your query.

But as Winfield said, better use Sphinx or Soir to do some efficient full text search.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜