Q:

Word to word search in rails

I have generated three different scaffold namely Education, Health, Jobs and all these contain five string each. How should i make a search that can search all these  strings.
For Example: My Education scaffold have a string called PRIMARY SCHOOL,
so if a user searches Best primary school in New York it should return with all the results submitted in the PRIMARY SCHOOL string.

A:

Whenever you try to do multi-table searching in Rails, you are sort of breaking certain ideas behind ActiveRecord::Base.
Preferably, you would enforce searching on individual tables.
Then for each model you can create a search method.
def self.search(query)
  query = "%#{query}%"
  where(
    'column_1 LIKE ?
  OR column_2 LIKE ?
  OR column_3 LIKE ?
  OR column_4 LIKE ?
  OR column_5 LIKE ?',
  query,
  query,
  query,
  query,
  query
  )
end

Otherwise, to achieve a multi table search you will need a handwritten query.
I would avoid this at all costs.
query = 'PRIMARY SCHOOL'
sql = <<SQL
  SELECT educations.*,
         healths.*,
         jobs.*,
  FROM educations,
       healths,
       jobs
  WHERE (each_table.each_column LIKE "%#{query}%")
SQL

records = ActiveRecord::Base.connection.execute(sql)

records.each do |record|
  # ...
end

This is a very unrecommended way of working with SQL in Ruby on Rails.

