Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert Advanced searching...

Replytotopic

Advanced searching

Posted in Forums : Ask a Rails expert

 
Profile

Authority 0
Posting Rating 69
Sign in to rate this post

Hi,

I’d like to build a search function on my website that searches multiple tables in my database.
I have the pages table and pages_categories table in which I’d like to search.

So the pages table looks like
- id
- parent_id
- page_category_id
- nl_title
- fr_title
- nl_content
- fr_content

and the pages_categories looks like
- id
- nl_title
- fr_title

At the moment, I can search in the pages table in it’s title or content.

A page can also be a parent (for example fruit), if it’s a parent the parent_id is empty.
There are also children (like apple, orange,..), the child has the parent_id, this is represented by the id of a parent.

/>SELECT * FROM `pages` WHERE (((nl_title LIKE 'fruit' OR nl_content LIKE 'fruit' OR nl_intro LIKE 'fruit')))
[/code]

So I’d like to search in the page_categories, in the pages and in the children of a page.

Some examples,
- if I search for cocktail and it’s a page_category, it has to show the pages that belong to cocktail.
- if I search for the parent fruit, I’d like to get all the fruits, apples, oranges, ...
- and 1 last one, if I search for a simple page, I’d like to find that also.

How can I do this on a rails way?

 
Www

Authority 87
Posting Rating 2
Sign in to rate this post

hi m rails developer.

15 days free trial, Web 2.0 web application using Ruby on Rails,Talentd ruby on rails professionals,cost effective development,Ruby on rails developer,Agile development processes, save almost 60% of your development cost,Open source and completely free application framework. http://www.railsteam.com

 
N595474161_9037

Authority 25
Posting Rating 29
Sign in to rate this post

Hi,

I think this is the query you needed..

Page.all(:conditions=>[‘nl_title like ? || nl_content like ? || nl_intro like ?’, ‘fruit’, ‘fruit’, ‘fruit’])

If u want to search from 2 tables related each other, see my blog..

http://srikanthjeeva.blogspot.com/2010/01/include-in-rails-query.html

Thanks,
Srikanth

 
Profile

Authority 0
Posting Rating 69
Sign in to rate this post

Thanks Srikanth, that works like a charm:)

 
436907d871deeaa04aa3f0cb4548587e

Authority 50
Posting Rating 55
Sign in to rate this post

If your site is going to have a lot of content and search is going to be prominent, you may want to consider adding a search engine such as SOLR or Sphinx.

I’ve used SOLR with Lucene in a previous project. Setting up and configuring the server is pretty straight forward. Search results are very fast, especially if you use the results from the search engine directly and not look up every result in your database.

 
Profile

Authority 50
Posting Rating 0
Sign in to rate this post

I too would recommend using a plugin if you want to easily search across multiple models. Thinking Sphinx is great if you have a large database, but needs some extra work to re-index the data periodically. For smaller projects, searchlogic is a good option since it’s really easy to get started with.

 
Www

Authority 87
Posting Rating 2
Sign in to rate this post

ya , ur idea is gud for starting base.
http://www.railsteam.com

 
Profile

Authority 25
Posting Rating 0
Sign in to rate this post

If you want query based searching then go for searchlogic and if you want full text search then go for acts_as_ferret. It is better than Sphnix. In older version of rails sphnix break the server.

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

I am testing your idea..

Prathap Rajamani

 
1cca05d8866731a685faa86bffbf334c

Authority 62
Posting Rating 69
Sign in to rate this post

sphinx and solr are both very robust indexed search options. i perfer acts_as_solr over acts_as_ferret for solr. thinking sphinx is an excellent gem for sphinx. i would give solr the nod simply due to the fact heroku doesn’t support sphinx and heroku rocks. solr does give you actual document indexing which may or may not come in handy. other than that i would say there are rather equal.

Replytotopic

Other Recent Topics

Ask a Rails expert : nested application ApplicationController get called intead of children::ApplicationController

Ask a Rails expert : Best way to structure a database for a large/static dataset

Ask a Rails expert : Ruby Developer (ROR) - Scottish based (Remote working from within the UK)

Ask a Rails expert : Above Ground Pool Supplies

Ask a Rails expert : How to get url params in observer or model in Rails 3.1

Ask a Rails expert : What can persuade you to hire Junior Ruby devs with significant PHP experience?

Ask a Rails expert : What industry value does the Ruby or Rails Certification currently have?

Ask a Rails expert : Louis Vuitton Damier Azur Canvas specially sale ( www.salecheaplouisvuitton.com )

Ask a Rails expert : ·How to check errors/puts statements from ruby files which are under cronob

Ask a Rails expert : Louis Vuitton cheap Soft Sided Luggagespecial offer( www.salecheaplouisvuitton.com )

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel