1

I am trying to implement a search feature to go across multiple tables. Right now I only have searching functionality for each of the individual tables in the database.

As an example I would like to be able to search the 'title' from multiple tables that all correspond to that title. The two tables in this example are 'acts' and 'attachments'.

Right now I have:

acts:

Controller

 def index
   if params[:search]
     @acts = Act.search(params[:search]).order('title DESC')
   else
     @acts = Act.all.order('title DESC')
   end
 end

Model

class Act < ActiveRecord::Base

  def self.search(query)
    where("title like ?", "%#{query}%")
  end
end

attachments:

Model:

class Attachment < ActiveRecord::Base
  def self.search(query)
    where("title like ?", "%#{query}%")
  end
end

Controller:

def index
  if params[:search]
    @attachments = Attachment.search(params[:search]).order('title DESC')
  else
    @attachments = Attachment.all.order('title DESC')
  end
end

Any help would be greatly appreciated. I am new to RoR and am trying to implement this the correct way.

Thank you.

segfaulter
  • 23
  • 4
  • check out this url i hope this will help http://stackoverflow.com/questions/2098131/list-databases-objects-using-rails-console – Amit Sharma Apr 22 '15 at 20:11

0 Answers0